Burbruee
Member
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.
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.
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
#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: