【发布时间】:2016-03-28 16:33:57
【问题描述】:
编辑:
问题出在 GoFish.h 文件中,在 constructor 中 具体来说,它试图实例化玩家对象。
编译器抛出以下错误消息:'Player'中没有名为'noOfBooks'的成员
GoFish() {players = new GoFishPlayer[2];} // Instantiate two players
对象切片对于初学者来说似乎是 OOP 中最模糊的概念之一。我一直在用 C++ 开发这款纸牌游戏,其中我有一个名为 Player 的基类和一个名为 GoFishPlayer 的派生类。当尝试访问引用回 Player 对象的 GoFishPlayer 对象的方法时,程序倾向于切掉派生类的特定方法和属性,从而使其成为基对象的克隆。有什么办法可以解决这个问题吗?
游戏.h
抽象类游戏:构成这两款游戏的基础 - 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
Derived Class GoFish - 当我尝试实例化从 Game Class 派生的 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() {}
....
【问题讨论】:
-
这么多代码——我们到底应该看什么?
-
导致对象切片的代码在哪里?
-
那些是指针 - 没有切片。
-
编译器抛出以下错误-“'Player'中没有名为'noOfBooks'的成员”,似乎找不到以下函数
-
好的 - 这不是切片问题,多态性就是这样工作的。
标签: c++ oop inheritance object-slicing