0

I was following internet tutorials on this topic, but I have the following situation:

I have a function with the following signature:

void func(long& rows, long& columns, int array[][columns]);

and I'm trying to use the function like this:

int matrix[5][4] = {0,  -1,  2,  -3,
                    4,  -5,  6,  -7,
                    8,  -9,  10, -11,
                    12, -13, 14, -15,
                    16, -17, 18, -19};

long rows = 5;
long columns = 4;

func(rows, columns, matrix);
^--- 'No matching function for call to 'func''

What is the problem? Why can't it call the function?

Oleksandr Novik
  • 489
  • 9
  • 24
  • You are trying to use a [variable-length array](https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard). These are a feature of C language, not supported in C++. A dynamically sized array in C++ is spelled `std::vector` – Igor Tandetnik Oct 30 '21 at 18:39
  • The problem is that this is not valid C++. All arrays in C++ have a fixed, constant size, and you are attempting to pass an array to a function whose size is specified by another parameter. C++ simply does not work this way. – Sam Varshavchik Oct 30 '21 at 18:46
  • You could use a template, with the sizes of the matrix as parameters. Be aware that you can't pass arrays in C++ though! All you can do is pass pointers or references to them. If the sizes are not compile-time constants, use a matrix class (like `std::vector` but 2D). – Ulrich Eckhardt Oct 30 '21 at 19:37

2 Answers2

1

Variable length arrays is not a standard C++ features.

You could declare the function and the array the following way

const size_t columns = 4;

void func( size_t rows, const int array[][columns]);

//...


int matrix[][columns] = { {  0,  -1,   2,  -3 },
                          {  4,  -5,   6,  -7 },
                          {  8,  -9,  10, -11 },
                          { 12, -13,  14, -15 },
                          { 16, -17,  18, -19 } };

func( sizeof( matrix ) / sizeof( *matrix ),  matrix);

//...

void func( size_t rows, const int array[][columns] )
{
    std::cout << rows << columns << array[0][1];
}

Pay attention to that as the number of columns is well-known there is no sense to pass it to the function. And moreover there is no sense to pass the number of rows and columns by reference.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Have you actually defined func in your program? The following source code compiles and works fine for me

#include <iostream>

#define ROW 5
#define COLUMN 4

void func(long &rows, long &columns, int array[][COLUMN]);

int main()
{
    int matrix[ROW][COLUMN] = {0, -1, 2, -3,
                          4, -5, 6, -7,
                          8, -9, 10, -11,
                          12, -13, 14, -15,
                          16, -17, 18, -19};

    long rows = 5;
    long columns = 4;

    func(rows, columns, matrix);
    return 0;
}

void func(long &rows, long &columns, int array[][COLUMN])
{
    std::cout << rows << columns << array[0][1];
}
  • You are using a defined value for columns, not the same as the original question. The issue was not function definition. – Zois Tasoulas Oct 30 '21 at 18:42
  • 1
    Two objections: Using macros instead of constants. Leaving magic number in the code instead of replacing them with their symbolic placeholders. – Ulrich Eckhardt Oct 30 '21 at 19:39