mardi 5 avril 2016

C++: Can't seem to resolve circular dependency

The Story

Hello. I'm currently working on a 3D game using OpenGL and c++ (gcc on ArchLinux, c++11). Up until yesterday, the project has been written in C. Today I decided to change the project to c++, so I could make use of the standard libraries, polymorhism and OOP. Everything has gone well.

The Issue

So, the game is controlled by the Game class. There is one instance of the game class and it can be accessed by using the function getGame (located in Game.h, same location as the Game class). So, input for the game is handled in the class InputManager (Located in InputManager.h). Now, the game class needs to include InputManager.h so it can have a InputManager object. But the InputManager object needs to include Game.h so it can get can a instance of the game object (So it can call the Game Object's quit function).

So as you can see, I have come across Circular Dependency. I have forward decleration, but that doesn't seem to work D:

If you could tell me how I can resolve this issue, that would be great. Here are some sources :)

Game.h

#ifndef _GAME_H
#define _GAME_H

#include <string>
#include <iostream>
#include <SDL2/SDL.h>
#include <GL/glew.h>
#include <stdexcept>
#include <InputManager.h>

 // The game class!!!
// This class holds all the objects, and global variables
// It Manages everything

class Game
{
public:
    // Constructors/Deconstructors
    Game(){}
    Game(std::string windowTitle, int width, int height);
    ~Game(){}

    // Public Functions
    void start();
    void quit();

    // Public variables
    SDL_Window *window;
    SDL_GLContext context;
private:
    // Private Functions
    bool init(std::string windowTitle);
    void update();

    // Private variables
    bool quitv;
    int width;
    int height;
    float dt;

    // Private Objects
    InputManager inputManager;
};

Game* getGame();

#endif

InputManager.h

#ifndef _INPUT_MANAGER_H
#define _INPUT_MANAGER_H

#include <SDL2/SDL.h>
#include <Game.h>

class InputManager
{
public:
    InputManager(){}
    InputManager(bool relative);

    void pollEvents();

    bool keys[1024];
private:
    SDL_Event event;

    void keydown();
    void keyup();
};

#endif

Thanks in advanced :)

Aucun commentaire:

Enregistrer un commentaire