I'm having a problem which I think has to do with the circular use of inlcuding header files.
The short version of my code would look a bit like this, I think:
GameEngine.h:
Player.h:
These are the errors I'm getting:
Player.h:37: error: ISO C++ forbids declaration of `GameEngine' with no type
Player.h:37: error: expected `;' before '*' token
If I change line 37 into GameEngine Game; (so it's not a pointer anymore), the error changes to:
Player.h:37: error: `GameEngine' does not name a type
If I remove line 37, a lot more errors are found:
Players\RandomP layer.cpp:12: error: expected class-name before '{' token
Players\RandomP layer.cpp:19: error: expected `,' or `...' before "note"
Players\RandomP layer.cpp:19: error: ISO C++ forbids declaration of `Notification' with no type
Players\RandomP layer.cpp:28: error: class `RandomPlayer' does not have any field named `Player'
Players\RandomP layer.cpp:34: error: `name' undeclared (first use this function)
Players\RandomP layer.cpp:34: error: (Each undeclared identifier is reported only once for each function it appears in.)
GameEngine.h:19 : error: `Player' has not been declared
GameEngine.h:19 : error: ISO C++ forbids declaration of `parameter' with no type
GameEngine.h:30 : error: ISO C++ forbids declaration of `Player' with no type
GameEngine.h:30 : error: expected `;' before '*' token
Player.cpp:38: warning: unused variable 'move'
:: === Build finished: 10 errors, 1 warnings ===
All of these errors seem to have to do with types like Player and GameEngine not being known. Does anybody have any idea how I can repair this?
Complete header files: (there are of course more files, so if it is necessary to see those to, just ask for them)
GameEngine.h:
Player.h:
Thanks for any help!
The short version of my code would look a bit like this, I think:
GameEngine.h:
Code:
#ifndef GAME_ENGINE
#define GAME_ENGINE
#include "Player.h"
class GameEngine
{
public:
GameEngine ();
// ..
private:
// ..
Player* players[2];
};
#endif
Code:
#ifndef PLAYER
#define PLAYER
#include "GameEngine.h"
// This class contains pure virtual functions!
class Player
{
public:
// ..
protected:
//..
private:
GameEngine* game; // line 37
// ..
};
#endif
Player.h:37: error: ISO C++ forbids declaration of `GameEngine' with no type
Player.h:37: error: expected `;' before '*' token
If I change line 37 into GameEngine Game; (so it's not a pointer anymore), the error changes to:
Player.h:37: error: `GameEngine' does not name a type
If I remove line 37, a lot more errors are found:
Players\RandomP layer.cpp:12: error: expected class-name before '{' token
Players\RandomP layer.cpp:19: error: expected `,' or `...' before "note"
Players\RandomP layer.cpp:19: error: ISO C++ forbids declaration of `Notification' with no type
Players\RandomP layer.cpp:28: error: class `RandomPlayer' does not have any field named `Player'
Players\RandomP layer.cpp:34: error: `name' undeclared (first use this function)
Players\RandomP layer.cpp:34: error: (Each undeclared identifier is reported only once for each function it appears in.)
GameEngine.h:19 : error: `Player' has not been declared
GameEngine.h:19 : error: ISO C++ forbids declaration of `parameter' with no type
GameEngine.h:30 : error: ISO C++ forbids declaration of `Player' with no type
GameEngine.h:30 : error: expected `;' before '*' token
Player.cpp:38: warning: unused variable 'move'
:: === Build finished: 10 errors, 1 warnings ===
All of these errors seem to have to do with types like Player and GameEngine not being known. Does anybody have any idea how I can repair this?
Complete header files: (there are of course more files, so if it is necessary to see those to, just ask for them)
GameEngine.h:
Code:
#ifndef GAME_ENGINE
#define GAME_ENGINE
#include <vector>
#include "Board.h"
#include "Player.h"
#include "Players.h"
class GameEngine
{
public:
GameEngine ();
GameEngine (std::vector<CellOwner>);
Board* getBoard () const;
int getTurns () const;
bool end () const;
bool setPlayer (const CellOwner, Player*);
bool removePlayer (const CellOwner);
void swapPlayers ();
void restart ();
void next ();
bool undo ();
bool redo ();
private:
unsigned int turn;
Board* board;
Player* players[2];
std::vector<unsigned int> moves;
};
#endif
Code:
#ifndef PLAYER
#define PLAYER
#include <string>
#include <list>
#include "GameEngine.h"
#include "Board.h"
enum Notification { CLEAR };
class Player
{
public:
virtual ~Player();
void addObserver (Player* observer);
void removeObserver (Player* observer);
void makeMove (const Board*);
std::string getName () const;
bool canLearn () const;
bool isHuman () const;
virtual void setName (const std::string name) = 0;
virtual void learn (const Board*, const int) = 0;
virtual void notify (const Notification) = 0;
virtual int thinkMove (const Board*) = 0;
virtual void giveTurn () = 0;
protected: // Protected, because observers can be asked for help
std::list<Player*> observers;
std::string name;
bool turn;
Player (const bool, const bool);
private:
GameEngine* game;
const bool human;
const bool teachable;
};
#endif
Comment