Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiler error C2653: not a class or namespace name

So I have been having this extremely frustrating problem lately with Visual C++ 2012. Up until a few hours ago, I was writing code just fine and everything was working as intended, until I decided to optimize some things and deleted a few classes. I fixed all of the errors that were popping up because of that, e.g. false includes, etc. Unfortunately, after this the VS compiler went crazy. It started giving me errors such as:

Error 14 error C2653: 'Class' : is not a class or namespace name

or even

Error 5 error C2143: syntax error : missing ';' before '}'
Error 4 error C2059: syntax error : '>'

I've checked multiple times, and everything is in it's right place: all headers included, all symbols placed where they should be.

As far as I understand, the problem is not with my code but with the compiler itself... Visual Studio can be really annoying at times, I guess. Anyway, I would really be grateful if someone could help me out on this one.

(By the way, disabling precompiled headers did not work)

Relevant parts of code:

Error 14:

#include "PlayerEntity.h"
PlayerEntity::PlayerEntity(void) {} // This line causes the error

Error 5:

class GameScreen : public BaseScreen
{
public:
    ...
private:
    ...
}; // This line causes the error

Error 4:

private:
     std::vector<BaseEntity*> _EntityList; // This line causes the error

Whole PlayerEntity.h file:

#ifndef PENTITY_H
#define PENTITY_H

#include "BaseEntity.h"

class PlayerEntity : public BaseEntity
{
public:
    PlayerEntity(void);
    PlayerEntity(float, float);
    virtual ~PlayerEntity(void);

    void render(sf::RenderWindow&);
    void update();
private:
    void init();
};

#endif

Whole GameScreen.h file:

#ifndef GSCREEN_H
#define GSCREEN_H

#include "BaseScreen.h"
#include "BaseEntity.h"
#include "PlayerEntity.h"

class GameScreen : public BaseScreen
{
public:
    GameScreen(sf::Vector2u&);
    virtual ~GameScreen(void);

    void start();
    void stop();

    void render(sf::RenderWindow&);
    void update(void);

    void addEntity(BaseEntity*);
    void destoryEntity(int id);
private:
    std::vector<BaseEntity*> _EntityList;
    sf::Vector2u _ScreenDimensions;
};

#endif

Whole BaseEntity.h file:

#ifndef BSENTITY_H
#define BSENTITY_H

#include "Input.h"
#include <SFML/Graphics.hpp>

class BaseEntity
{
public:
    BaseEntity(void);
    virtual ~BaseEntity(void);

    sf::Vector2f position;

    virtual void update(void);
    virtual void render(sf::RenderWindow&);
    void compare(BaseEntity*);
protected:
    sf::Texture *_EntityTexture;
    sf::Sprite  _EntitySprite;

    bool _isAlive;
    int  _id; 

    virtual void init();
};

#endif

Whole Input.h file:

#ifndef INPUT_H
#define INPUT_H

#include "ScreenSystem.h"
#include <SFML/Window.hpp>

class Input
{
public:
    Input(void);
    Input(sf::RenderWindow*);
    virtual ~Input(void);

    static bool keyPressed(int);
    static bool keyReleased(int);

    static bool mouseHeld(int);
    static bool mouseReleased(int);
private:
    static sf::RenderWindow *_Window;
};

#endif

Whole ScreenSystem.h file:

#ifndef GHANDLER_H
#define GHANDLER_H

#include "BaseScreen.h"
#include "MenuScreen.h"
#include "GameScreen.h"
#include <SFML/Window.hpp>

class ScreenSystem
{
public:
    ScreenSystem(void);
    ScreenSystem(sf::RenderWindow*);
    virtual ~ScreenSystem(void);

    BaseScreen *getCurrentScreen(void);
    void setScreen(int);
private:
    int _currentScreenID;

    std::vector<BaseScreen*> _Screens;
    sf::RenderWindow *_Window;
};

#endif
like image 537
valtari Avatar asked Apr 01 '13 09:04

valtari


2 Answers

You have a circular dependency in your headers. BaseEntity.h includes Input.h, which includes ScreenSystem.h, which includes GameScreen.h, which in turn re-includes BaseEntity.h. This leads to class names appearing before they are declared, causing compilation failure.

To avoid this, do not include headers unnecessarily. For example, do not include Input.h from BaseEntity.h, since it's not needed at all; and do not include BaseScreen.h from ScreenSystem.h since only a declaration class BaseScreen; is needed, not the complete class definition.

Also, check that you do not have duplicate header guards. Some of them do not match the header name (e.g. GHANDLER_H for ScreenSystem.h), which makes me think that they may have been accidentally copied from other headers. Finally, don't use reserved names like _EntitySprite for your own symbols; for simplicity, avoid leading or double underscores.

like image 68
Mike Seymour Avatar answered Nov 06 '22 22:11

Mike Seymour


Did you copy the error messages into your question or did you retype them? Because error 14 has 'Class' with a capital C which is almost certainly not right.

Also, you should use as few include directives in your header files as possible. For example, GameScreen doesn't use PlayerEntity, so you can remove that include and BaseEntity is only used via pointer so you can replace

#include "BaseEntity.h"

with a forward declaration

class BaseEntity;
like image 35
IronMensan Avatar answered Nov 06 '22 23:11

IronMensan