-1

I'm trying to make chess in the c++ console and I have a function that searches for the piece you want to move. The first parameter is the 2d array that stores the board's state. But when I call the function in the move function it gives me this error:

cannot convert argument 1 from 'int' to 'int [][8]

It also tells me that

argument of type "int" is incompatible of type "int (*)[8]

I don't understand why that is since the argument is the same 2d array

pair<int, int> search_piece(int v[8][8], int col, int piece, bool colour) {
    for (int row = 0; row < 8; row++)
        if (!colour) {
            if (v[row][col] == piece && v[row][col] < 7)
                return make_pair(row, col);
        }
        else
            if (v[row][col] == piece && v[row][col] > 6)
                return make_pair(row, col);
    return make_pair(-1, -1);
}


void move(int v[8][8], int row, int col, int piece, bool colour) {
    pair<int, int> pos = search_piece(v[8][8], col, piece, colour);
    int irow = pos.first;
    int icol = pos.second;
    if (irow >= 0 && icol >= 0)
        swap(v[irow][icol], v[row][col]);
    else
        cout << "Invalid move!\n";
}
Strasse34
  • 1
  • 2
  • 2
    You want `search_piece(v, col, piece, colour);`, not `v[8][8]` – Jon Reeves Jun 30 '22 at 15:38
  • First of all, where do you get the error? Is it the full and complete compiler output? Please try to create a [mre] and add comments on the lines where you get the errors. – Some programmer dude Jun 30 '22 at 15:38
  • 2
    In `search_piece(v[8][8], col, piece, colour);` the `v[8][8]` is a single `int` that is past the bounds of the array. You are not attempting to pass an array. The compiler is complaining that it expected an array but you gave it an int. – drescherjm Jun 30 '22 at 15:39
  • 1
    Secondly, what about the error message is unclear? What do your text-books say about arrays, and how to pass arrays to functions? How do you pass arrays to your `move` function, for example? How does that differ from how you attempt to pass arrays to your `search_piece` function? – Some programmer dude Jun 30 '22 at 15:39

1 Answers1

0

search_piece function gets a two-dimensional array as its first argument, but in the move function where you call it, you just pass a single integer not an array. v[8][8] in pair<int, int> pos = search_piece(v[8][8], col, piece, colour); is a single element of v array. if you want to pass hole array simply pass v.

pair<int, int> pos = search_piece(v, col, piece, colour);