Error: expected class-name before '{' token

Hi guys

I was once again working on a Framework using inheritance when I encountered the following error:

C:\..\C++ & SDL2\SDL Game Development\Player.h|10|error: expected class-name before '{' token

I've researched a bit on the inet. The error was often in combination with a missing #include. So I went on and put all of my header-files within the Player class but I ended up with the same error. Here are the files that I think are related to the Problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef _PLAYER_H_
    #define _PLAYER_H_


#include "SDLGameObject.h"
#include "LoaderParams.h"


class Player : public SDLGameObject
{ //error Points to here
    public:
        Player(const LoaderParams* pParams);
        ~Player();

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




#endif // _PLAYER_H_ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#ifndef _SDLGAMEOBJECT_H_
    #define _SDLGAMEOBJECT_H_

#include "GameObject.h"
#include "LoaderParams.h"
#include "Game.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 // _SDLGAMEOBJECT_H_  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 #ifndef _GAMEOBJECT_H_
    #define _GAMEOBJECT_H_

#include <string>
#include "SDL.h"
#include "TextureManager.h"
#include "LoaderParams.h"


class GameObject
{
    public:
        virtual void update();
        virtual void draw(SDL_Renderer* pRenderer);
        virtual void clean();

    protected:
        GameObject(const LoaderParams* pParams);
        virtual ~GameObject();

        std::string m_textureID;
        int m_currentFrame;
        int m_currentRow;
        int m_x;
        int m_y;
        int m_width;
        int m_height;
};

#endif // _GAMEOBJECT_H_ 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 #ifndef _LOADERPARAMS_H_
    #define _LOADERPARAMS_H_

class LoaderParams
{
    public:
        LoaderParams(int x, int y, int width, int height, std::string textureID) : m_x(x), m_y(y), m_width(width), m_height(height), m_textureID(textureID) {};
        int getX() const { return m_x; }
        int getY() const { return m_y; }
        int getWidth() const { return m_width; }
        int getHeight() const { return m_height; }
        std::string getTextureID() const { return m_textureID; }
    private:
        int m_x;
        int m_y;
        int m_width;
        int m_height;
        std::string m_textureID;
};

#endif // _LOADERPARAMS_H_ 


Hope someone's able to help me out here :)

Greetz HalfNOoB
Line 7 of 'LoaderParams.h', that semicolon at the end of the constructor definition shouldn't be there.
Thanks for your correction. I’ve deleted the semicolon but funnily I’m still getting the error at the exact same point :/

Any idea where else I’m wrong? Should I upload the .cpp files as well?
Please upload the .cpp file that you were compiling when the error occured.
Also, you didn't upload Game.h. The error could be there.
I agree with doug4, please copy and paste 'Game.h'. I very much doubt that the error is in any of the .cpp files though. Also, you should include the string header in 'LoaderParams.h'.
Here we go

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#ifndef _GAME_H_
    #define _GAME_H_

#include "SDL.h"
#include "SDL_image.h"
#include <iostream>
#include <vector>
#include <memory>
#include "TextureManager.h"
#include "Player.h"
#include "LoaderParams.h"

class Game
{
    public:
        ~Game() {}

        static Game* Instance() { return &m_Instance; }

        // simply set the running variable to true
        bool init(const char* title, int xpos, int ypos, int width,int height, int flags);
        void render();
        void update();
        void handleEvents();
        void clean();

        // a function to access the private running variable
        bool running() { return m_bRunning; }

        SDL_Renderer* getRenderer() const { return m_pRenderer; }


    private:
        Game() {}
        static Game m_Instance;

        SDL_Window* m_pWindow;
        SDL_Renderer* m_pRenderer;

        int m_currentFrame;

        bool m_bRunning;

        std::vector<GameObject*> m_gameObjects;
};

typedef TextureManager TheTextureManager;
typedef Game TheGame;

#endif //_GAME_H_ 
I have a feeling that this is going to go on for a while. You can either post 'TextureManager.h' and 'SDL_Renderer.h' next, or better yet link us to the tutorial that you are pulling this from. I recognize the code, but it's been way too long and I don't remember where I saw it.
Yep. The I'm retyping this from SDL Game Development by Shaun Mitchell.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#ifndef _TEXTUREMANAGER_H_
    #define _TEXTUREMANAGER_H_

#include "SDL.h"
#include "SDL_image.h"
#include <iostream>
#include <map>

class TextureManager
{
    public:
        bool load(std::string fileName,std::string id,SDL_Renderer* pRenderer);

        void draw(std::string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE);
        void drawFrame(std::string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE);

        std::map <std::string, SDL_Texture*> m_textureMap;

        static TextureManager* Instance() { return &m_Instance; }

    private:
        TextureManager() { }

        static TextureManager m_Instance;

        SDL_Rect srcRect;
        SDL_Rect destRect;

};



#endif // _TEXTUREMANAGER_H_ 
I think you're missing a forward declaration, this part is a total shot in the dark but I think it might be for the 'SDL_Renderer' object. Add this to your top most header file:

 
class SDL_Renderer;


That book catches a lot of flak on Amazon for having annoying little errors in the code. Most people say that it isn't for beginners.
Thanks for your work. I'm well aware that the book is being criticized and quite error prone. I've already had to fix quite a few to get this Point. The book leaves out some parts and it's definitly unusable for a total beginner. However, my experience with coding is quite limited as well.


The SDL_Renderer is as far as my knocklegde of SDL goes, included in the normal Header (SDL.h) and a normal class like SDL_Window for e.g.
In Player.h, SDLGameObject and GameObject, you only need a forward declaration to LoaderParams. So you can use the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#ifndef _PLAYER_H_
    #define _PLAYER_H_

#include "SDLGameObject.h"

class LoaderParams; // not really needed because it's already
                    // declared in GameObject.h, but can't hurt

class Player : public SDLGameObject
{
    public:
        Player(const LoaderParams* pParams);
        ~Player();

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

#endif // _PLAYER_H_  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#ifndef _SDLGAMEOBJECT_H_
    #define _SDLGAMEOBJECT_H_

#include "GameObject.h"
#include <string>

class LoaderParams; // not really needed for same reasons

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 // _SDLGAMEOBJECT_H_   


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 #ifndef _GAMEOBJECT_H_
    #define _GAMEOBJECT_H_

#include <string>
#include "SDL.h" // for SDL_Renderer

class LoaderParams;

class GameObject
{
    public:
        virtual void update();
        virtual void draw(SDL_Renderer* pRenderer);
        virtual void clean();

    protected:
        GameObject(const LoaderParams* pParams);
        virtual ~GameObject();

        std::string m_textureID;
        int m_currentFrame;
        int m_currentRow;
        int m_x;
        int m_y;
        int m_width;
        int m_height;
};

#endif // _GAMEOBJECT_H_  
You have a circular dependency.

Game.h includes Player.h
Player.h includes SDLGameObject.h
SDLGameObject.h includes Game.h

To avoid this problem I recommend only using include for your own headers if you need to. If you only have pointers and references to a class a forward declaration is enough. doug4 has shown you how to do this.

You will probably have to add the includes in the source files (.cpp) instead, because the full class definition is needed when you create objects, call functions and in other ways use the members of the class.
Last edited on
Topic archived. No new replies allowed.