编译器错误C2653:不是类或命名空间名称 [英] Compiler error C2653: not a class or namespace name

查看:5119
本文介绍了编译器错误C2653:不是类或命名空间名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

或甚至

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.

据我所知,问题不在于我的代码,而是与编译器本身... Visual Studio可以真的很烦人的时候,我猜。无论如何,我真的很感激,如果有人可以帮助我在这一个。

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)

部分代码:

错误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

Whole PlayerEntity.h文件:

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文件:

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文件:

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文件:

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文件:

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


推荐答案

在您的头中有循环依赖。 BaseEntity.h 包括 Input.h ,其中包括 ScreenSystem.h ,其中包括 GameScreen.h ,它反过来重新包括 BaseEntity.h 。这会导致类名称在声明之前出现,导致编译失败。

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.

为避免这种情况,请勿不必要地包含标题。例如,不要包括 BaseEntity.h 中的 Input.h ,因为它根本不需要;并且不包括 BaseScreen.h ScreenSystem.h ,因为只有一个声明 class BaseScreen; ,而不是完整的类定义。

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.

此外,请检查您没有重复的标题保护。其中一些不匹配头名称(例如 GHANDLER_H for ScreenSystem.h ),这让我认为他们可能已被意外从其他标头复制。最后,不要使用保留的名称,如 _EntitySprite 为您自己的符号;为了简单起见,避免引导或双下划线。

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.

这篇关于编译器错误C2653:不是类或命名空间名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆