编辑:
问题出在GoFish.h文件中,具体来说是在构造函数中,它试图实例化玩家对象。
编译器抛出以下错误消息:“Player”中没有名为“noOfBooks”的成员
GoFish() {players = new GoFishPlayer[2];} // Instantiate two players
对于初学者来说,对象切片似乎是 OOP 中最模糊的概念之一。我一直在用 C++ 开发这款纸牌游戏,我有一个名为Player的基类和一个名为GoFishPlayer的派生类。当尝试访问引用回 Player 对象的 GoFishPlayer 对象的方法时,程序倾向于切掉派生类的特定方法和属性,从而使其成为基对象的克隆。有没有办法克服这个问题?
游戏.h
抽象类 Game :它构成了这两个游戏的基础 - GoFish 和 CrazyEights
class Game {
protected:
Deck* deck;
Player* players;
int player_id;
public:
Game(){
deck = Deck::get_DeckInstance(); // Get Singleton instance
player_id = choosePlayer();
players = NULL;
}
....
}
GoFish.h
派生类 GoFish - 当我尝试实例化从游戏类派生的 Player 对象时,问题出现在构造函数中
class GoFish : public Game{
static GoFish* goFish;
GoFish() {players = new GoFishPlayer[2];} // Instantiate two players
public:
static GoFish* get_GoFishInstance() {
if(goFish == NULL)
goFish = new GoFish();
return goFish;
}
播放器.h
class Player{
protected:
std::string playerName;
Hand hand;
bool win;
public:
Player(){
playerName = "Computer"; // Sets default AI name to Computer
hand = Hand(); // Instatiate the hand object
win = false;
}
....
GoFishPlayer.h
class GoFishPlayer : public Player {
private:
std::vector <int> books;
int no_of_books;
public:
GoFishPlayer() {
no_of_books = 0;
books.resize(13);
}
int noOfBooks(){return no_of_books;}
void booksScored() {no_of_books++;}
bool checkHand() {}
....