1
votes

I have to make a snake and ladders game with classes (MyGame, Board, Player, Dice). MyGame needs all the other classes at some point or another thus I have the headers for the other classes in the MyGame.h file. Yet I get 3 errors that read: Line 18 -----"error: ‘Board’ has not been declared." Line 18 -----"error: ‘Player’ has not been declared." Line 19 -----"error: ‘Player’ has not been declared."

An object MyGame is initialized in my main (skanes.cpp), and then inside the function MyGame::start() the other objects are created. I thought that maybe the classes Board or Player require something from MyGame in order to be build thus cycling but Player and Board are not dependent of MyGame besides the initialization of the obejct. HELP!

MyGame.h

#ifndef MYGAME_H
#define MYGAME_H
#include "Board.h"
#include "Player.h"
#include "Dice.h"

#include "Player.h"
class MyGame
{

    protected:
        static const int numPlayers = 2;

    public:
        MyGame();
        ~MyGame();
        void start();
        void play(Player[], Dice, Board);  <-------Line 18
        void win(Player[]);                <-------Line 19
        int getNumPLayers();
};

#endif

MyGame.cpp

#include <iostream>
#include <vector>
#include "MyGame.h"
#include "Board.h"
#include "Player.h"
#include "Dice.h"

MyGame::MyGame()
{

}

MyGame::~MyGame()
{

}

void MyGame::start()
{
    Board brd;
    Player plyr[numPlayers];
    Dice dc;
    while (plyr[0].getPosition() != brd.getBoardSize() && plyr[1].getPosition() != brd.getBoardSize() && plyr[numPlayers - 1].getTurn() <= plyr[numPlayers - 1].getMaxTurn())
        play(plyr, dc, brd);

    win(plyr);

}

void MyGame::play(Player p[], Dice d, Board b)
{

        for (int i = 0; i < b.getBoardSize(); i++)
        {
            p[i].setPosition(d.roll());
            if(p[i].getPosition() > b.getBoardSize())
            {
                p[i].setPosition( (b.getBoardSize() - p[i].getPosition()) * 2 );
            }

            if (b.getType(p[i].getPosition()) == 'S')
                p[i].setPosition(-b.getSnakeLadderMove());
            else if (!b.getType(p[i].getPosition()) == 'L')
                p[i].setPosition(b.getSnakeLadderMove());

            p[i].setTurn();
        }

}

void MyGame::win(Player p[])
{
    for (int i = 0; i > numPlayers; i++) 
    {
        if (p[i].getPosition() == 30)
            std::cout << "Payer " << i << "wins!!" << std::endl;
    }
}

Board.h

#ifndef BOARD_H
#define BOARD_H
#include "MyGame.h"

class Board
{
    public:
        Board();
        ~Board();
        bool getType(int);
        int getNumeber(int);
        int getSnakeLadderMove();
        int getBoardSize();

    private:
        struct tile
        {
            char type;
            int number;
        };

        static const int boardSize = 30;
        static const int snakeLadderMove  = 3;
        tile place[boardSize];
};

#endif

Board.cpp

#include <stdlib.h>
#include <time.h>
#include "Board.h"

Board::Board()
{
    int count = 0;
    //initialize random seed to randomize snakes and ladders.
    srand(time(NULL));

    for (int k = 0; k < boardSize; k++)
    {
        place[k].type = 'N';
        place[k].number = k + 1;
    }

    while(count <= 3)
    {
        int index = rand() % boardSize + 1;

        while (index < 4)
        {
            index = rand() % boardSize + 1;
            // Makes sure it only replaces tiles with type = 'N'
            while(getType(index) != 'N')
                index = rand() % boardSize + 1;
        }

        place[index].type = 'S';

        while (index > boardSize - 3)
        {
            index = rand() % boardSize + 1;
            // Makes sure it only replaces tiles with type = 'N' 
            while(getType(index) != 'N')
                index = rand() % boardSize + 1;
        }   

        place[index].type = 'L';
        count++;

    }
}

Board::~Board()
{

}

int Board::getNumeber(int index)
{
    return place[index].number;
}

bool Board::getType(int index)
{
    return place[index].type;
}

int Board::getBoardSize()
{
    return boardSize;
}

Player.h

#ifndef PLAYER_H
#define PLAYER_H
#include "MyGame.h"
#include "Board.h"

class Player
{
    public:
        Player();
        ~Player();
        void setPosition(int);
        void setTurn();
        int getPosition();
        int getTurn();
        int getMaxTurn();
        int getNumPlayers();

    private:
        static const int maxTurn = 20;
        int position;
        int turn;
};

#endif

Player.cpp

#include <iostream>
#include "Player.h"
#include "Board.h"

Player::Player()
{

    /* 
        In order for the setters to work position and turn
        have to be equal to 1;
    */
    position = 1;
    turn = 1;
}

Player::~Player()
{

}

void Player::setPosition(int move)
{
    //Assumes constructor setted the value to 0
    position += move;
    ;
}

void Player::setTurn()
{
    //Assumes constructor sette4d the value to 0
    turn++;
}


int Player::getPosition()
{
    return position;
}

int Player::getTurn()
{
    return turn;
}

int Player::getMaxTurn()
{
    return maxTurn;
}

Dice.h

#ifndef CDADO_H_INCLUDED
#define CDADO_H_INCLUDED

#include <ctime>
#include <cstdlib>

class Dice{

    public:

        Dice();
        int roll();

};


#endif

Dice.cpp

#include <iostream>
#include "Dice.h"

using namespace std;


Dice::Dice()
{
    srand(time(0));
}


int Dice::roll()
{
    return  (rand() % 6) + 1;
}  

skanes.cpp //It was supposed to be snakes.

#include <iostream>
#include "MyGame.h"

using namespace std;

int main()
{
    MyGame snakes;

    snakes.start();
}
2

2 Answers

0
votes
#define BOARD_H
#include "MyGame.h"  // <--- here lies the problem

Do not include MyGame.h in Board.h and Player.h. You have a circular dependency.

0
votes

Pretend that you are a C++ compiler that's compiling your Player.cpp:

#include <iostream>
#include "Player.h"

At this point the compiler starts reading Player.h:

#ifndef PLAYER_H
#define PLAYER_H
#include "MyGame.h"

Now your C++ compiler goes to read MyGame.h. Remember that this is all that your compiler has processed up to now. It has not processed anything else.

In MyGame.H there's another #include "Player.H", however it does absolutely nothing whatsoever, since the include guard was defined, so the second inclusion of Player.H becomes a big fat nothing.

Your compiler continues to process MyGame.H, and finds a reference to some mysterious class named Player that has never been defined anywhere. That's the explanation for your compilation error.

There does not appear to be any need for Player.H to include MyGame.H, so just get rid of that include.

It's a circular reference that's completely unneeded, and easily fixable by getting rid of it. If you do need real circular references between header files, your C++ textbook should have a good explanation of what forward references are, and how to use them.