Expected class-name before ” token. Circular file connection

  c++

I am learning SDL and trying to make a game. Everything was fine until I started using polymorphism. I read an article
on this topic and understand what is required of me, but for the third day I cannot find where in my code I make this mistake. I will be very grateful for any hint and help.

I have an abstract class "GameObject.h". From it I inherit the "SDLGameObject.h" class, which is the parent of the "Player.h" and "Enemy.h" classes. The "Player.h" and "Enemy.h" classes are included in the "Game.h" file. And here, as it seems to me, what the problem is: for the "SDLGameObject.h" class to work normally, I need to include the "Game.h" file into it.

I’ve read that a forward declaration should be used, but can’t figure out where I need to do this.

Game.h

#pragma once

#include <vector>
#include <string>
#include <SDL2/SDL.h>

#include "Player.h"
#include "Enemy.h"

class Game
{
private:
    Game() {}
    static Game *instance;

    SDL_Renderer* renderer;
    SDL_Window* window;

    std::vector<GameObject*> game_objects;

    bool running;
public:
    ~Game() {}
    static Game *Instance()
    {
        if (instance != nullptr)
            return instance;
        else
        {
            instance = new Game();
            return instance;
        }
    }

    bool init(std::string name, int x, int y, int width, int height, int flags = 0);
    void update();
    void handle_events();
    void draw();
    void clear();
    bool is_running() { return running; }

    SDL_Renderer *get_renderer() { return renderer; }
};

typedef Game TheGame;

GameObject.h

#pragma once

#include <stdio.h>
#include "LoaderParams.h"
#include "TextureManager.h"

class GameObject 
{
public:
    virtual void draw()=0;
    virtual void update()=0;
    virtual void clean()=0;
protected:
    GameObject (const LoaderParams* params) {}
    virtual ~GameObject() {}
};

SDLGameObject

#pragma once

#include "GameObject.h"     
#include "Game.h"

class SDLGameObject : public GameObject
{
protected:
    int x;
    int y;
    int w;
    int h;

    int current_row;
    int current_frame;

    std::string texture_id;

public:
    SDLGameObject(const LoaderParams *params);
    virtual void draw();
    virtual void update() {};
    virtual void clean() {};
};

Player.h

#pragma once

#include "SDLGameObject.h"

class Player : public SDLGameObject
{
public:

    Player (const LoaderParams *params);

    void draw();
    void update();
    void clean();
};

Enemy.h

#pragma once

#include "SDLGameObject.h"

class Enemy : public SDLGameObject
{
public:

    Enemy (const LoaderParams *params);

    void draw();
    void update();
    void clean();
};

The "GameObject.h" file also includes the "LoaderParams.h" and "TextureManager.h" files, which in turn do not include anything else.
The .cpp files include only the corresponding .h files.

This is the error I am getting:

g++ -std=c++11 -Wall -g -o obj/Enemy.o -c src/include/Enemy.cpp
In file included from src/include/Game.h:7,
                 from src/include/SDLGameObject.h:4,
                 from src/include/Enemy.h:3,
                 from src/include/Enemy.cpp:1:
src/include/Player.h:6:1: error: expected class-name before ‘{’ token
    6 | {
      | ^
make: *** [Makefile:47: obj/Enemy.o] Error 1

Source: Windows Questions C++

LEAVE A COMMENT