0

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.

Quentin
  • 62,093
  • 7
  • 131
  • 191
JohnDoe
  • 179
  • 1
  • 4
  • 17
  • You need to make sure that the header for Field does not include the header for GameEngine. If it does you have a loop. That must be broken. Thankfully you don't need to include GameEngine.h in Field.h – drescherjm Dec 24 '17 at 15:04
  • Possible duplicate of [Resolve build errors due to circular dependency amongst classes](https://stackoverflow.com/questions/625799/resolve-build-errors-due-to-circular-dependency-amongst-classes) – drescherjm Dec 24 '17 at 15:08
  • As stated by @drescherjm you've almost certainly got a circular dependency loop. Having said that... based on the code shown there shouldn't be any need to `#include ` in `Field.h` as `GameEngine` is only ever referred to by reference. A forward declaration of `GameEngine` in `Field.h` should suffice. – G.M. Dec 24 '17 at 15:09
  • I'v been trying to put just a forward declaration as you said like 10 times since yesterday and it crashed everytime untill now. But yeah, it works now, somehow. Well, never had a project that big and it starts to get a bit overwhelming for me. Thanks anyways. – JohnDoe Dec 24 '17 at 15:09
  • You can achive what you want. Just don't have each Field.h include GameEngine.h. It does not need to do so. – drescherjm Dec 24 '17 at 15:11
  • ***it crashed everytime untill now*** That would be a different bug. – drescherjm Dec 24 '17 at 15:14

0 Answers0