以前に作成した関数ポインタへのアドレスを宣言できず、何が間違っているのか理解できません。
bool
a を返し、 1 つの整数引数を取る関数への関数ポインターを宣言しました。
場合に応じて、関数ポインターは 2 つの関数のいずれかを指す必要があります:bool is_black
またはbool 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;
}
}