0

I'm having a problem with circular dependencies, I suppose this is a design flaw from introducing the Game class in the wrong way.

Game.h:

#pragma once
#include <SFML\Graphics.hpp>

#include "GameScreen.h"
#include "TitleScreen.h"

class Game
{
protected:
    sf::RenderWindow window;

    GameScreen* CurrentGameScreen;
    TitleScreen Title;

public:
    Game(void);
    ~Game(void);

    sf::RenderWindow getWindow(void);

    void Run();
    void Close();
};

GameScreen.h:

#pragma once

#include "Game.h"

class GameScreen
{
public:
    GameScreen(void);
    ~GameScreen(void);

    virtual void LoadAllResources() {};
    virtual void DrawScreen(Game* game) {};
    virtual void Update(Game* game) {};
};

TitleScreen.h:

#pragma once
#include <SFML\Graphics.hpp>

#include "GameScreen.h"

class TitleScreen : public virtual GameScreen
{
private:
    sf::Texture title_screen;
    sf::Sprite titleScreen;

    sf::Font font;
    sf::Text menuExit;

public:
    TitleScreen(void);
    ~TitleScreen(void);

    void LoadAllResources();
    void DrawScreen(Game* game);
    void Update(Game* game);
};

Then there's the main file:

#include "Game.h"

int main()
{
    Game game;
    game.Run();

    //sf::RenderWindow window(sf::VideoMode(800, 600), "Test");

    //GameScreen* currentScreen;
    //TitleScreen titleScreen;

    //currentScreen = &titleScreen;

    //while (window.isOpen())
    //{
    //  currentScreen->Update(&window);
    //  currentScreen->DrawScreen(&window);
    //}

    return 0;
}

GameScreen.h and TitleScreen.h raise a handful of C2061. From what I understand these are caused by circular dependencies between Game.h and Gamescreen.h.

TitleScreen.h is giving me error C2504: 'GameScreen' : base class undefined.

Game.h: on lines 12 and 13, give C2143: syntax error : missing ';' before '*', although I'm not sure where this is coming from and my IDE is not giving me any syntax errors.

If I remove the #include statement from GameScreen.h and substitute it with a forward declaration class Game; (which I guess breaks the circular dependency?) most of the above is solved, but TitleScreen.cpp throws a set of C2027, C2227 and C2228 (undefined type, left of -> and left of .) every time I try to access a Game object. IntelliSense points out that a pointer to an incomplete class is not allowed.

I got it working before introducing the Game class - DrawScreen() and Update() would take as argument a pointer to window (sf::RenderWindow* window). There's part of the old code left in main.cpp.

1
  • 1
    As answered @piokuc, you need a Forward declaration Commented May 7, 2013 at 13:32

1 Answer 1

4

In the GameScreen.h you should declare the Game class instead of including its whole header file, so this:

class Game;

instead of:

#include "Game.h"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.