0

So I've got a bit of a problem (well two but they're unrelated to each other).

I have two headers that look as follows:

Game.h

#ifndef INIT_GAME_H
#define INIT_GAME_H

#include <deps/deps.h>

#include <handlers/RenderHandler.h>

class Game {
    private:
        RenderHandler* renderHandler; /* <-- This is line 32 in my actual header */

    public:
        Game() {};
        ~Game() {};

        int initialise();
        void handleEvents();
        void update();
        void render();
        void clean();
}; // class Game
#endif // INIT_GAME_H

RenderHandler.h

#ifndef HANDLERS_RENDERHANDLER_H
#define HANDLERS_RENDERHANDLER_H

#include <init/Game.h>

class RenderHandler {
    private:
        Game* game;

    public:
        RenderHandler() {};
        ~RenderHandler() {};

        void initialise(Game* game);
        void render();
}; // class RenderHandler
#endif // HANDLERS_RENDERHANDLER_H

But the above gives me an error during compilation:

game.h(32): error C2143: syntax error: missing ';' before '*'
game.h(32): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
game.h(32): error C2238: unexpected token(s) preceding ';'

As you've probably guessed, I'm trying to store Game's instance in RenderHandler and vice versa. I'm probably doing it completely the wrong way but I can't figure why it's not working.

Also, all ; are in their right places prior to line 32 in my header file.

EDIT:

after doing the suggested forward declaration, I get the following error (now in RenderHandler.cpp file).

Error: pointer to incomplete class type is not allowed

This is what my code file looks like

RenderHandler.cpp

#include <handlers/RenderHandler.cpp>

void RenderHandler::initialise(Game* game) {
    this->game = game;
}

void RenderHandler::render() {
    glfwSwapBuffers(game->getPrimaryWindow());
}
  • 6
    possible duplicate of [Proper way to #include when there is a circular dependency?](http://stackoverflow.com/questions/3901606/proper-way-to-include-when-there-is-a-circular-dependency) – Barmar Sep 04 '15 at 13:02
  • 1
    use forward declaration in the files – RC Brand Sep 04 '15 at 13:02

1 Answers1

5

Use forward declaration :

#ifndef HANDLERS_RENDERHANDLER_H
#define HANDLERS_RENDERHANDLER_H

// FW declaration of Game
class Game;

class RenderHandler {
    private:
        Game* game;

    public:
        RenderHandler() {};
        ~RenderHandler() {};

        void initialise(Game* game);
        void render();
}; // class RenderHandler
#endif // HANDLERS_RENDERHANDLER_H
VB_overflow
  • 1,763
  • 11
  • 15
  • 1
    Or, of course, the opposite and forward declare the `RenderHandler` class in `Game.h`. :) – Some programmer dude Sep 04 '15 at 13:10
  • @Joachim: Yeah, I would tend to think that what you propose would make more sense – VB_overflow Sep 04 '15 at 13:21
  • but in doing this, will I still get access to functions in the declared class? –  Sep 04 '15 at 14:02
  • The proper way to do it is the following (taking my initial proposition as an example) : you FW declare `class Game` in the .h of `RenderHandler`, so you cannot access any functions of `Game` in code you would write in `RenderHandler.h`. However, in `RenderHandler.cpp` file you will include `Game.h` so you will have access to `Game` functions from there. If you are not using templates it is generally better to avoid writing any code in header files. – VB_overflow Sep 04 '15 at 14:09