[solved] C++ inheritance, header includes. Help needed.


Burbruee

Member
Joined
Sep 28, 2006
Messages
302
Location
Skåne, Sweden.
Website
Visit site
Hello. I'm currently reading a book on SDL game development. But I'm having some issues as the book doesn't say what headers to include in which files. Just the class {}; part

Here's some related code.

Player.h


#ifndef PLAYER_H
#define PLAYER_H
 
#include "SDLGameObject.h"
 
class Player : public SDLGameObject
{
public:
    Player(const LoaderParams* pParams);
    
    virtual void draw();
    virtual void update();
    virtual void clean();
};
 
#endif
SDLGameObject.h


#ifndef SDLGAMEOBJECT_H
#define SDLGAMEOBJECT_H
 
#include "Game.h"
#include "GameObject.h"
#include "TextureManager.h"
 
class SDLGameObject : public GameObject
{
public:
    SDLGameObject(const LoaderParams* pParams);
    
    virtual void draw();
    virtual void update();
    virtual void clean();
    
protected:
    int m_x;
    int m_y;
    
    int m_width;
    int m_height;
    
    int m_currentRow;
    int m_currentFrame;
    
    std::string m_textureID;
};
 
#endif
GameObject.h


#ifndef GAMEOBJECT_H
#define GAMEOBJECT_H
 
#include "LoaderParams.h"
 
class GameObject
{
public:
    virtual void draw() = 0;
    virtual void update() = 0;
    virtual void clean() = 0;
    
protected:
    GameObject(const LoaderParams* pParams)
    {
        
    }
    
    virtual ~GameObject()
    {
        
    }
};
 
#endif
Game.h


#ifndef GAME_H
#define GAME_H

#include <SDL2/SDL.h>
#include <vector>
#include "TextureManager.h"
#include "GameObject.h"
#include "Player.h"
 
 
class Game
{
public:
    static Game* Instance();
    ~Game();
    
    bool init(const char* title, int xpos, int ypos, int width, int height, bool fullscreen);
    void render();
    void update();
    void handleEvents();
    void clean();
    
    SDL_Renderer* getRenderer() const { return m_pRenderer; }
    
    bool Running();
    
private:
    Game();
    
    static Game* s_pInstance;
    
    SDL_Window* m_pWindow;
    SDL_Renderer* m_pRenderer;
    
    GameObject* m_go;
    GameObject* m_player;
    
    std::vector<GameObject*> m_gameObjects;
    
    bool m_bRunning;
    int m_currentFrame;
    
};
 
typedef Game GameEngine;
 
#endif

The problem comes when I include Player.h in Game.h. Where I will create a new Player() object in the bottom of init. And because SDLGameObject.h already includes Game.h for its getRenderer() function the compiler get's confused. (at least that's what I think is the problem)

But well, I can't remove the Game.h from SDLGameObject since I need that function from Game in there.

my compiler gives me the error that SDLGameObject in Player.h is unknown. If I don't include Player.h in Game.h it compiles fine, but then I can't make a new player.
 
Last edited by a moderator:
Why can't you make a new player if you don't include Player.h in Game.h? I assume the code for init() is in Game.cpp, just include Player.h there.

If you really need to get around circular dependencies in C++ you can do it by making a forward declaration of the type. That is, name the types before you use them like:

Code:
class Player;
Just keep in mind that this only works if you're declaring pointers or references to the forward-declared type, and it doesn't work if you do anything with the type that isn't opaque (like calling one of its methods or referencing one of its member variables). For those things the compiler has to know more about the type than what its name is.
 
Last edited by a moderator:
Why can't you make a new player if you don't include Player.h in Game.h? I assume the code for init() is in Game.cpp, just include Player.h there.


If you really need to get around circular dependencies in C++ you can do it by making a forward declaration of the type. That is, name the types before you use them like:


class Player;
Just keep in mind that this only works if you're declaring pointers or references to the forward-declared type, and it doesn't work if you do anything with the type that isn't opaque (like calling one of its methods or referencing one of its member variables). For those things the compiler has to know more about the type than what its name is.
It works, thanks.

I didn't think about including it in the source file. Usually I just let the classes include their respective header and nothing more, and let the rest of the includes go in the header. 
 
Back
Top