tuan long
tuan long

Reputation: 603

Problems with multidimension arrays in C++ again

I passed a multidimension array to a function in a reverse call like this:

template<int size>
void print_multidimension_array(int A[][size], int &cnt){
  if(cnt <= 0){
       return;
  }
  else{
      int (*B)[size];
      print_multidimension_array(B, cnt--);
  }

}
int main(int argc, const char * argv[])
{
  int A[3][3] = {{1,2,3}, {4,5,6}, {7,8,9}};
  int count = 5;
  print_multidimension_array(A, count);
}

I didn't get any compilation error but after I put it into running I was told that "no matching function to call 'print_multidimension_array'".Thanks for any help or suggestions.

Upvotes: 3

Views: 126

Answers (2)

Mike Seymour
Mike Seymour

Reputation: 254761

cnt-- returns a copy of the old value of cnt. That is a temporary rvalue which can't be bound to a reference - so it can't be used as the second argument to your function.

I suspect you want --cnt. That gives a lvalue referring to the new value, which can be bound to a reference.

Taking the argument by value would fix the compiler error; but you'd still need to pre-decrement to avoid an infinite loop.

Upvotes: 9

David Schwartz
David Schwartz

Reputation: 182883

Change:

void print_multidimension_array(int A[][size], int &cnt){

to:

void print_multidimension_array(int A[][size], int cnt){

Upvotes: 3

Related Questions