1

I get GameObject: base class undefined while trying to compile. I don't see anything wrong. I have a parent-child relationship between GameObject and player:

GameObject.h

#pragma once
#include "Game.h"

class GameObject
{
protected:
    int x, y;

    SDL_Texture* objTexture;
    SDL_Rect srcRect{}, destRect{};

public:
    GameObject(const char* textureSheet, int p_x, int p_y);
    ~GameObject();

    void Update();
    void Render();
};

Player.h

#pragma once
#include "GameObject.h"

class Input;

class Player : public GameObject
{
private:
    Input* input{ nullptr };
public:
    Player(Input * p_input);
    ~Player();

    void Update();
};

This is Game.h

#pragma once
#include <SDL.h>
#include <SDL_image.h>
#include "TextureManager.h"
#include "Player.h"
#include "Map.h"
#include "Input.h"

class Player;
class Map;
class Input;

class Game
{
private:
    SDL_Window* window{ nullptr };

    Player* player{ nullptr };
    Input* input{ nullptr };

    Map* map{ nullptr };

    bool isRunning = false;

public:
    Game();
    Game(const char* title, int xPos, int yPos, int width, int height, bool fullscreen);
    ~Game();

    void HandleEvents();
    void Update();
    void Render();
    void clean();
    bool running() { return isRunning; };
    
    static SDL_Renderer* renderer;
};

most of the discussion I have seen says that this is due to some repeated includes which create circular dependencies, but I don't see any problem with that. is this something to do with the game class?

Adamska_01
  • 27
  • 4

2 Answers2

1

It would help if you gave us the exact error message and pointed to the line number.

I suspect this is because your GameObject has a constructor that requires arguments but your Player class does not provide these arguments in its constructor initialization list.

But this is just a guess.

A new guess! The #include "Game.h" in your GameObject.h is the problem.

Only ever include what you use, and never create circular references even if you use #pragma once That pragma doesn't do what some people think it does.

Instead of Game.h include the SDL headers you require.

Whenever possible remove header includes from header files and use forward declarations of anything that is a pointer. Include the header for the concrete definitions only in your source files (the .cpp files).

You really want to avoid the problem of including one header and having it pull in a huge web of 500 other include files.

You are going to need the SDL header in order to use SDL_Rect though.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • the error message is C2504 and it is on line 7 of GameObject.h – Adamska_01 Nov 17 '20 at 17:03
  • This is the player's constructor: Player::Player(Input* p_input) : GameObject("Assets/WARRIOR_Idle.png", 0, 0), input(p_input) { } – Adamska_01 Nov 17 '20 at 17:05
  • @Adamska_01 The int x, y line? Seems unlikely. – Zan Lynx Nov 17 '20 at 17:13
  • sorry, line 7 of Player.h* – Adamska_01 Nov 17 '20 at 17:19
  • @Adamska_01 I edited my answer with a new idea. – Zan Lynx Nov 17 '20 at 17:19
  • Thanks, but the thing is that I need the static SDL_Renderer* from Game.h (last line) in order for gameObject to work. If that is the issue, I think I need to change my design – Adamska_01 Nov 17 '20 at 17:41
  • @Adamska_01: SDL_Renderer is a pointer so you can forward declare it as `class SDL_Renderer` which will inform the compiler that the class exists somewhere, and as long as you never do anything which uses the definitions in the class, the compiler is cool with it. That means pointers, references and return values are OK with a forward declaration only. Then be sure to include the actual class definitions in the source file. – Zan Lynx Nov 17 '20 at 17:51
0

The easy way is to forward declare the missing class prototypes.

class SDL_Texture;
class SDL_Rect;

As you have already started on in Player.h

A more defensive way is to always include all headers needed in every source file.

#include <SDL.h>

or another SDL header.

And you have already solved the circular include with the

#pragma once
Surt
  • 15,501
  • 3
  • 23
  • 39