语义问题:调用'displayBoard'没有匹配函数

Semantics Issue: No matching function for call to 'displayBoard'

本文关键字:函数 displayBoard 问题 语义 调用      更新时间:2023-10-16

我只是用一个2D数组作为参数来调用一个函数。我不明白为什么它告诉我没有函数可调用,我可以看到函数。这是我代码开头的原型:void displayBoard(int [][COLS], int);这是调用displayBoard的函数和displayBoard函数:

void playerTurn()
{
    char board[ROWS][COLS] = {{'*', '*', '*'}, {'*', '*', '*'}, {'*', '*', '*'}};
    char row, col;
    displayBoard(board, ROWS);
    cout << "Player X's Turn.nEnter a row and a column to place an X.nRow: ";
    cin >> row;
    cout << "nColumn: ";
    cin >> col;
    //clear screen
    //edit contents of 2D array
    displayBoard(board, ROWS);
    cout << "Player O's Turn.nEnter a row and a column to place an X.nRow: ";
    cin >> row;
    cout << "nColumn: ";
    cin >> col;
    //Validate each user's move (make sure there isn't an x or o already there
    //Ask for a re-input is validation fails
}
void displayBoard(const char board[][COLS], int ROWS)
{
    cout << setw(14) << "Columns" << endl;
    cout << setw(14) << "1 2 3 " <<  endl;
    cout << "Row 1:  " << board[0][0] << " " << board[0][1] << " " << board[0][2] << endl;
    cout << "Row 2:  " << board[1][0] << " " << board[1][1] << " " << board[1][2] << endl;
    cout << "Row 3:  " << board[2][0] << " " << board[2][1] << " " << board[2][2] << endl;
    cout << endl;
}

它在playerTurn函数的两个调用中都给出了错误。我不明白我做错了什么。

您之前已经声明了函数定义

void displayBoard(int [][COLS], int);

然后在playerTurn()中,您调用

displayBoard(board, ROWS);

这里boardchar board[ROWS][COLS],所以编译器查看它是否看到了一个声明或定义的名为displayBoard的函数,该函数接受了这些参数。由于编译器没有看到它,它将发出一个错误。

要解决此问题,您需要将降级更改为

void displayBoard(const char board[][COLS], int ROWS);

或者,您可以更改函数的顺序,并在displayBoard() 之前定义playerTurn()

playerTurn中的board包含char,但displayBoard中的参数希望它包含int