0

我无法为之前创建的函数指针声明地址,我无法理解我做错了什么。

bool我声明了指向返回 a并接受 1 个整数参数的函数的函数指针。

根据具体情况,函数指针应指向以下两个函数之一:bool is_blackbool is_white将指针分配给is_black函数的地址时没有错误,但是当我尝试对is_white做同样的事情时,我得到了这个错误。

通过尝试在下一个函数(' void cover_fields ')中做同样的事情,我没有得到任何错误。

bool is_white(int piece)
{
    if (piece > 0)
        return true;
    return false;
}
bool is_black(int piece)
{
    if (piece < 0)
        return true;
    return false;
}
bool is_mate(int** board, bool is_white=true)
{
    bool(*is_enemy)(int enemy_piece); //<-- HERE I DECLARED FUNCTION POINTER
    int KING;

    if (is_white)
    {
        is_enemy = &is_black;
        KING = WHITE_KING;
    }
    else
    {
        is_enemy = &is_white; //<-- ERROR ON THIS LINE
        KING = BLACK_KING;
    }

    short int col, row, king_col, king_row;

    //REST OF THE FUNCTION BELOW
    //finding king coordinates
    for (row = 0; row < 8; row++)
    {
        for (col = 0; col < 8; col++)
        {
            short int piece = board[row][col];
            if (piece == KING)
            {
                king_col = col;
                king_row = row;
                break;
            }
        }
    }
    
    if (!is_checked(board, false))
        return false;

    //If there is at least one free field that king can move to, return false
    //UP
    row = king_row - 1; col = king_col;
    if (field_is_valid(row , col) && 
        (board, row, col, false) 
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    // UP - RIGHT
    row = king_row - 1; col = king_col + 1;
    if (field_is_valid(row, col) && king_is_not_near(board, row, col, is_white)
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    //RIGHT
    row = king_row; col = king_col + 1;
    if (field_is_valid(row, col) && king_is_not_near(board, row, col, is_white)
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    //RIGHT-DOWN
    row = king_row+1; col = king_col + 1;
    if (field_is_valid(row, col) && king_is_not_near(board, row, col, is_white)
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    //DOWN
    row = king_row + 1; col = king_col;
    if (field_is_valid(row, col) && king_is_not_near(board, row, col, is_white)
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    //LEFT-DOWN
    row = king_row + 1; col = king_col - 1;
    if (field_is_valid(row, col) && king_is_not_near(board, row, col, is_white)
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    //LEFT
    row = king_row; col = king_col - 1;
    if (field_is_valid(row, col) && king_is_not_near(board, row, col, is_white)
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    //LEFT-UP
    row = king_row - 1; col = king_col - 1;
    if (field_is_valid(row, col) && king_is_not_near(board, row, col, is_white)
        && is_enemy(board[row][col] && !is_checked(board, is_white)))
        return false;

    return true;
}

// FUNCTION IN WHICH THERE IS NO ERROR EVEN THOUGH I DID THE SAME THING
void cover_fields(int** board, int row, int col, int row_dir, int col_dir,int moves, int piece_is_white)
/* Function covers all the fields on the board in certain direction
   direction is declared with row_dir, col_dir values that can be -1,0,+1
 */
{
    // Pointer to the function that is going to call the next move
    bool(*is_enemy_piece)(int enemy_piece); // DECLARING FUNCTION POINTER

    int PIECE = board[row][col];
    if (piece_is_white)
        is_enemy_piece = &is_black;
    else
        is_enemy_piece = &is_white; // <-- NO ERROR ON THIS LINE

    //REST OF THE FUNCTION
    short int _row = row; short int _col = col;
    /* _row & _col are temp values for row and col.
    *  they are getting increased every while itteration
        by the direction value: row_dir, col_dir
    */

    while (field_is_valid(_row + row_dir, _col + col_dir) &&
        (board[_row + row_dir][_col + col_dir] == EMPTY_FIELD ||
            is_enemy_piece(board[_row + row_dir][_col + col_dir])))
    {
        next_move(board, row, col, _row + row_dir, _col + col_dir, PIECE, piece_is_white, moves);
        _row += row_dir; _col += col_dir;
    }
}
4

1 回答 1

4

你有一个参数来is_mate命名is_white。这个局部变量将隐藏同名的全局函数(任何对is_white参数的引用,而不是函数)。

解决方案包括将参数名称更改为不同的名称(例如enemy_is_white),或完全限定函数名称:

    is_enemy = &::is_white;
于 2021-09-06T19:21:08.070 回答