I'm writing Monopoly game with C++ and SFML. My application already has around 20 classes and they work together pretty well but I can't find solution for one problem. First I need to show you a bit of my code, I'm sorry if I post too much code (Ill try to paste simple minimum), but problems happen all accross my application once I add 1 line of code - #include "GameEngine.h"
into Field.h and I dont really know what. Let's start with heart of application, GameState class that has GameLoop inside
class GameState : public State {
sf::Clock m_clock;
sf::Sprite m_background;
GameEngine m_gameEngine;
public:
GameState();
void initialise();
void handleUserInput();
void update(sf::Time);
void draw();
};
GameState::GameState(std::shared_ptr<ApplicationData> data) : m_gameEngine(2, "John", "Richard") {
//2 players named John and Richard, just for tests now
}
Then class that handles all the logic, GameEngine
class GameEngine {
//players
std::vector<Player> m_players;
int m_numberOfPlayers;
int m_activePlayer = 0;
//game objects
GameBoard m_gameBoard;
public:
GameEngine(int, std::string, std::string);
GameBoard& getBoard() { return m_gameBoard; }
Player& getActivePlayer() { return m_players[m_activePlayer]; }
};
For now, it just creates two players
GameEngine::GameEngine(int numberOfPlayers, std::string playerOneName, std::string playerTwoName)
: m_numberOfPlayers(numberOfPlayers), m_firstDice(FIRST_DICE_POSITION_X, FIRST_DICE_POSITION_Y),
m_secondDice(SECOND_DICE_POSITION_X, SECOND_DICE_POSITION_Y) {
m_players.push_back(Player(playerOneName, sf::Color::Red, -5, 0));
m_players.push_back(Player(playerTwoName, sf::Color::Blue, -5, 0));
}
Player class is nothing special, few variables and getter and setter functions
class Player {
int m_positionID = 0;
std::string m_name;
sf::CircleShape m_token;
};
Another significant part is GameBoard
class GameBoard {
std::array<std::shared_ptr<Field>, BOARD_SIZE> m_board;
public:
GameBoard();
~GameBoard() = default;
std::shared_ptr<Field>& getField(size_t index) { return m_board[index]; }
};
And finnaly Field
where all the problems start
class Field {
protected:
int m_positionID;
float m_positionX;
float m_positionY;
bool m_buyable;
public:
Field() {}
Field::Field(int positionId, float positionX, float positionY, bool buyable) :
m_positionID(positionId), m_positionX(positionX), m_positionY(positionY), m_buyable(buyable) { }
virtual ~Field() = default;
virtual void calculateCharge(GameEngine&) = 0;
};
Field is base class for all my fields in game like Property, Start, GoToJail, etc. I want something like that: player rolls the dice, he is moved to new position, this position is checked (inside GameEngine)
m_board.getField(m_gameEngine.getActivePlayer().getPositionID())->calculateCharge();
Now calculateCharge()
will perform a bit different action for every class, simple runtime polymorphism, but I want this function to actually call some functions from inside GameEngine
, thats why I want to pass GameEngine& into this function, but once I '#include GameEngine.h' into 'Field.h', I got
error C2504: 'Field': base class undefined
followed by several errors from derived classes, that they cannot access members (which are protected), etc, whole inheritance isnt working anymore, and after few errors I got
fatal error C1903: unable to recover from previous error(s); stopping compilation
I tried to add forward declaration but it doesnt help. I dont really know how to fix this.