面向对象编程的新手。我正在尝试制作一个 connect 4 游戏,其中包含一个用于整个游戏的类,其中包含一个棋盘对象,然后包含两个玩家对象。我已经意识到我需要我的玩家能够从棋盘上访问一些信息,特别是行和列的长度(也使用指针以便用户可以编辑棋盘大小),所以我试图将一个棋盘对象传递给那些具体函数作为参数。我收到了上述未声明的错误,但我只是对为什么我只将它用作参数时需要在播放器类中声明它感到困惑。
例如我得到这个错误: 错误:'board' 尚未声明 25 | int takeTurn(棋盘和我的棋盘); | ^~~~~
这是我的 player.h 文件中的一些代码:
#ifndef PA02_TEMPLATE_PLAYER_H
#define PA02_TEMPLATE_PLAYER_H
#include "board.h"
class player{
private:
protected:
char piece;
int playerMove[2];
int playerNum;
public:
player()= default;
player(char p, int num){
piece = p;
playerNum = num;
}
int takeTurn(board& myBoard);
void colChoice(int i);
void findRow(board& myBoard, int rows);
void setPlayerNum(int i){
playerNum = i;
}
void setPiece(char p){
piece = p;
}
char getPiece(){
return piece;
}
int getPlayerNum(){
return playerNum;
}
};
class aiPlayer : player{
private:
int aiDifficulty = 2;
public:
void setDifficulty(int i){
aiDifficulty = i;
}
int getDifficulty(){
return aiDifficulty;
}
void takeTurn(int rows, int COLS, board& myBoard);
vector<int> minimax(board& miniBoard, int depth, int alpha, int beta, bool maximizingPlayer);
int scorePosition(char piece, char opponents_piece, board& scoringBoard);
};
#endif //PA02_TEMPLATE_PLAYER_H