I'm creating the a Card game c++ program and I got some problem. PLZ help!!
I got few files:
Main.cpp (ask user which card game he want to play)
Card.h (creat rank and suit for cards)
War.h, War.cpp (creats War game)
TicTacToe.h, TicTacToe.cpp (creats TicTacToe game)
Game.h, Game.cpp (required to have a virtual function play() that all of the games inherit. Requires to use polymorphism to allow a single variable of type *Game() to access and play any of your games.

Here is my Main.cpp:

int main()
{
    int response =0;
    Game *myGame;

    while(response !=3)
    {
        cout<<"Press 1 to play War\n";
        cout<<"press 2 to play TicTacToe\n";
        cout<<"press 3 to quit\n";
        cin>>response;

        switch(response)
        {
            case 1:myGame = new War();
            break;
            case 2:myGame = new TicTacToe();
            break;
            default:return 0;
        }
        myGame->play();
    }
    delete myGame;
}

First question: Since I use 'new' operator to create War or TicTacToe. When I build it, it said 'can not convert TicTacToe* to Game* in assignment. What's wrong with that?

Here is my virtual function play() in Game.h:(nothing in there, Idk if I'm wrong)

virtual void play(){};

and I have no idea the construction of each War and TicTacToe should look like.
So here is my code:

Card.h

class Card
{
private:
    int rank;       //the number on the card
                    //jack=11; queen=12; king=13; ace=14
    CardSuit suit;  //the suit of the card

public:
    Card(int r, CardSuit s);//constructor
    int getRank() const;   //returns the card's rank
    void setRank(int);
    void setSuit(int);
    void display();         //displays the card to cout
                            //e.g.  if rank=11 and suit=HEARTS
                            //display  Jh

};

Card::Card(int r, CardSuit s)
{
    rank=r;
    suit=s;
}

War.h and constructor:

class War: public Game
{
    public:
        War();
        ~War();
        void play();
    private:
        Card *hc;
        Card *cc;


};

War::War() //constructor
{
    hc = new Card(2,HEARTS);
    cc = new Card(2,HEARTS);
}

Here is TicTacToe.h (it dosen't need to use Card class):

class TicTacToe
{
    public:
        TicTacToe();
        virtual ~TicTacToe();
        void printInstructions();
        void initializeBoard(char*);
        void displayBoard(char*);
        char winner(char*);
        bool isLegal(char*, int);
        void humanMove(char*);
        void computerMove(char *);
        void announceWinner(char winner);
        void play();
    protected:
    private:
    //The Tic-Tac-Toe board - a 9 character array
    //Each character can be an X, O, or a space
    //The space represents an open space on the board
    char * pb;
    char response; //User's response
    char turn; //keeps track of who's turn it is
};

TicTacToe::TicTacToe()  //constructor
{
    pb = new char[9];
}

Thanks for helping me debug! I really appreciate it!

Recommended Answers

All 6 Replies

See this line?
class War: public Game

You're declaring War to be a derived class of Game.

See this line?
class TicTacToe

You're declaring TicTacToe to be a class of its own with no base class.

Oh! also, When I implement iheretance like this:

class TicTacToe : public Game
{...

It's said
expected class-name before ' { ' token
What is that? What does that mean?

See this line?
class War: public Game

You're declaring War to be a derived class of Game.

See this line?
class TicTacToe

You're declaring TicTacToe to be a class of its own with no base class.

Yes I want them both to be the derive class of Game. but there is always an error message :

expected class-name before ' { ' token

Idk how to deal with that...

can anyone help.

Where are you defining your Game class and, if it's in a header file, do both TicTacToe.h and War.h #include it?

OK, let me rephrase that. Your Game class SHOULD be in a header file. And both TicTacToe.h and War.h SHOULD #include it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.