编译器错误C2653:不是类或命名空间名称

时间:2013-04-01 09:33:35

标签: c++ visual-studio-2012

所以我最近在使用Visual C ++ 2012时遇到了这个非常令人沮丧的问题。直到几个小时前,我编写的代码很好,所有内容都按预期工作,直到我决定优化一些内容并删除一些类。我修复了因此而突然出现的所有错误,例如假包括等等。不幸的是,在此之后VS编译器发疯了。它开始给我错误,如:

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

甚至

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

我已多次检查过,所有内容都在正确的位置:包含所有标题,所有符号都放在应有的位置。

据我了解,问题不在于我的代码,而在于编译器本身...我猜,Visual Studio有时会非常烦人。无论如何,如果有人能帮我解决这个问题,我真的很感激。

(顺便说一下,禁用预编译的标头工作)

代码的相关部分:

错误14:

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

错误5:

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

错误4:

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

整个PlayerEntity.h文件:

#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

整个GameScreen.h文件:

#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

整个BaseEntity.h文件:

#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

整个Input.h文件:

#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

整个ScreenSystem.h文件:

#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

2 个答案:

答案 0 :(得分:37)

您的标头中存在循环依赖关系。 BaseEntity.h包括Input.h,其中包含ScreenSystem.h,其中包含GameScreen.h,后者又会重新包含BaseEntity.h。这导致类名在声明之前出现,导致编译失败。

要避免这种情况,请不要不必要地包含标头。例如,请勿在{{1​​}}中添加Input.h,因为根本不需要BaseEntity.h。并且不包括BaseScreen.h中的ScreenSystem.h,因为只需要声明class BaseScreen;,而不是完整的类定义。

另外,请检查您是否没有重复的标头防护。其中一些与标题名称不匹配(例如GHANDLER_H的{​​{1}}),这使我认为它们可能是从其他标题中意外复制的。最后,不要将ScreenSystem.h之类的保留名称用于自己的符号;为简单起见,避免引导或双下划线。

答案 1 :(得分:3)

您是否将错误消息复制到了您的问题中,或者您是否重新输入了这些消息?因为错误14具有&#39; Class&#39;用大写C几乎肯定不对。

此外,您应该尽可能少地使用头文件中的include指令。例如,GameScreen不使用PlayerEntity,因此您可以删除包含,而BaseEntity仅用于指针,因此您可以替换

#include "BaseEntity.h"

带有前瞻性声明

class BaseEntity;