0

編集

  1. 問題はGoFish.hファイルにあります。具体的にはコンストラクターで、プレイヤー オブジェクトをインスタンス化しようとしています。

  2. コンパイラは次のエラー メッセージをスローします: 「プレーヤー」に「noOfBooks」という名前のメンバーはありません

GoFish() {players = new GoFishPlayer[2];} // Instantiate two players

オブジェクトのスライスは、初心者にとって OOP で最もあいまいな概念の 1 つです。私は 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 - Game クラスから派生した 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;
}

Player.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() {}
....
4

1 に答える 1