dimanche 10 octobre 2021

C++ How to pass empty array as default argument in a function

I'm new to C++ and I want to create a function that takes an empty array of a certain size as default value. Basically the array should act like a storage for the iterative function calls and the function will return some value in that array at the end. I need an empty array because my function is going to be recursive.

I tried the following code but it didn't work, the problem lies with int& M[a][b] and I don't know what's the correct way to define my empty array.

#include<iostream>

void foo(const int& a, const int& b, int (&M)[a][b]){
    std::cout<<a<<'\n';
}
int main(){
    int M[2][3];
    foo(2,3,M);
}

Here's the error message

 error: cannot bind rvalue '(int)((int (*)[3])(& M))' to 'int&'

Aucun commentaire:

Enregistrer un commentaire