1

I'm a newbie when it comes to C++ and coding in general as this is only my second course. For my assignment, I have to code a program for a memory game where the user selects cards in a 2D array to try to match them. Right now, however, I'm struggling to get my function call for initializeBoard() to compile without an error. The message I get is:

    main.cpp:63:3: error: no matching function for call to
       'initializeBoard'
      initializeBoard(gameBoard, rows);

    main.cpp:35:6: note: candidate function not viable: no known
       conversion from 'int [rows][col]' to 'int (*)[col]' for 1st
       argument

A simplified copy of my code is written below containing the lines I believe are involved in the error.

    int rows = 4;
    int col = 4;

    void initializeBoard(int gameBoard[][col], int rows);

    int main()
    {
       int players;
       string p1Name, p2Name, p3Name, p4Name, p5Name;

       rules();

       getPlayers(players, p1Name, p2Name, p3Name, p4Name, p5Name);

       if (2 < players && players < 5)
       {
         rows = 6;
         col = 6;
       }
       else if (players == 5)
       {
         rows = 8;
         col = 8;
       }

       int (gameBoard)[rows][col];

       initializeBoard(gameBoard, rows); // I get an error saying there is no matching function for call to 'initializeBoard'
       }

Could someone explain to me where I am going wrong and help me find a solution to the issue?

itemp
  • 11
  • 2
  • Pass a pointer to the first cell, and the maximum rows and maximum columns. This assumes the array is contiguous slots. – Thomas Matthews Apr 06 '21 at 23:44
  • So I need to pass the rows and col variables as int parameters separate from the array then? And yes, the end result should look more like a table than actual separate cards – itemp Apr 06 '21 at 23:49
  • Not exactly. The function receiving the 2d array needs to know the capacity of the array, in rows and columns. – Thomas Matthews Apr 06 '21 at 23:51
  • 1
    Note: C++ doesn't have Standard support for Variable Length Arrays like `int (gameBoard)[rows][col];` so you might not like what you get. What I generally do is work in 1 dimensional arrays all nicely wrapped up in a class that hides the math that converts the 1D array in to 2 dimensions and carries around the dimensions so no one else has to. [Here's a pretty good, simple example](https://stackoverflow.com/a/2076668/4581301). – user4581301 Apr 07 '21 at 00:14

1 Answers1

0
const int rows = 4;
const int col = 4;

void initializeBoard(int gameBoard[][col], int rows);

Here you've defined a function that will take a 2D array (by pointer) in which the second dimension is 4. The number of rows is not fixed, but you're passing that dimension as the second parameter.

It looks like in main you're trying to pass a 2D array that doesn't have columns (or second dimension) of size 4. Hence the compiler error. The second dimension must be 4 in order to call initializeBoard.

Although your compiler is allowing you to declare a variable sized 2D array, this isn't allowed by the C++ standard. It's a compiler specific extension.

Instead of using raw arrays like in C, you could use a vector of vectors. That will allow you to pass a 2D matrix:

#include <vector>

void initializeBoard(std::vector<std::vector<int>> &gameBoard);

Alternatively, you could convert your function into a template function that will deduce the dimensions for you:

template<size_t row, size_t col>
void initializeBoard(int (&gameBoard)[row][col])
{
 ...
}

However, the array dimensions should be known at compile time. If they're not, you're better off using a vector of vectors.

jignatius
  • 6,304
  • 2
  • 15
  • 30