Note that there are some explanatory texts on larger screens.

plurals
  1. POC++: circular dependency issue
    primarykey
    data
    text
    <p>I'm having a problem with circular dependencies, I suppose this is a design flaw from introducing the Game class in the wrong way.</p> <p>Game.h:</p> <pre><code>#pragma once #include &lt;SFML\Graphics.hpp&gt; #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(); }; </code></pre> <p>GameScreen.h:</p> <pre><code>#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) {}; }; </code></pre> <p>TitleScreen.h:</p> <pre><code>#pragma once #include &lt;SFML\Graphics.hpp&gt; #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); }; </code></pre> <p>Then there's the main file:</p> <pre><code>#include "Game.h" int main() { Game game; game.Run(); //sf::RenderWindow window(sf::VideoMode(800, 600), "Test"); //GameScreen* currentScreen; //TitleScreen titleScreen; //currentScreen = &amp;titleScreen; //while (window.isOpen()) //{ // currentScreen-&gt;Update(&amp;window); // currentScreen-&gt;DrawScreen(&amp;window); //} return 0; } </code></pre> <p><em>GameScreen.h</em> and <em>TitleScreen.h</em> raise a handful of <strong>C2061</strong>. From what I understand these are caused by circular dependencies between Game.h and Gamescreen.h. </p> <p><em>TitleScreen.h</em> is giving me <strong>error C2504: 'GameScreen' : base class undefined</strong>.</p> <p><em>Game.h</em>: on lines 12 and 13, give <strong>C2143: syntax error : missing ';' before '*'</strong>, although I'm not sure where this is coming from and my IDE is not giving me any syntax errors.</p> <p>If I remove the <code>#include</code> statement from GameScreen.h and substitute it with a forward declaration <code>class Game;</code> (which I guess breaks the circular dependency?) most of the above is solved, but <em>TitleScreen.cpp</em> throws a set of <strong>C2027</strong>, <strong>C2227</strong> and <strong>C2228</strong> (<strong>undefined type</strong>, <strong>left of -></strong> and <strong>left of .</strong>) every time I try to access a Game object. IntelliSense points out that a <em>pointer to an incomplete class is not allowed</em>. </p> <p>I got it working before introducing the Game class - <code>DrawScreen()</code> and <code>Update()</code> would take as argument a pointer to window (<code>sf::RenderWindow* window</code>). There's part of the old code left in main.cpp.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload