C++ 编译器错误 C2653:不是类或命名空间名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15740952/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compiler error C2653: not a class or namespace name
提问by valtari
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:
所以我最近在使用 Visual C++ 2012 时遇到了这个非常令人沮丧的问题。直到几个小时前,我还在编写代码,一切都按预期工作,直到我决定优化一些东西并删除一些类。我修复了因此而出现的所有错误,例如错误包含等。不幸的是,在此之后 VS 编译器发疯了。它开始给我错误,例如:
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.
据我了解,问题不在于我的代码,而在于编译器本身......我想 Visual Studio 有时真的很烦人。无论如何,如果有人能帮助我解决这个问题,我将不胜感激。
(By the way, disabling precompiled headers did notwork)
(顺便说一句,禁用预编译头没有不工作)
Relevant parts of code:
相关代码部分:
Error 14:
错误 14:
#include "PlayerEntity.h"
PlayerEntity::PlayerEntity(void) {} // This line causes the error
Error 5:
错误 5:
class GameScreen : public BaseScreen
{
public:
...
private:
...
}; // This line causes the error
Error 4:
错误 4:
private:
std::vector<BaseEntity*> _EntityList; // This line causes the error
Whole PlayerEntity.h file:
整个 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
Whole GameScreen.h file:
整个 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
Whole BaseEntity.h file:
整个 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
Whole Input.h file:
整个 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
Whole ScreenSystem.h file:
整个 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
回答by Mike Seymour
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
,其中包括ScreenSystem.h
,其中包括GameScreen.h
,而后者又重新包括BaseEntity.h
。这会导致类名出现在声明之前,从而导致编译失败。
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.
为避免这种情况,请不要包含不必要的标题。例如,不要包含Input.h
from BaseEntity.h
,因为它根本不需要;并且不包括BaseScreen.h
fromScreenSystem.h
因为只需要声明class BaseScreen;
,而不是完整的类定义。
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.
另外,请检查您是否没有重复的标题守卫。其中一些与标题名称不匹配(例如GHANDLER_H
for ScreenSystem.h
),这让我认为它们可能是从其他标题中意外复制的。最后,不要像_EntitySprite
你自己的符号那样使用保留名称;为简单起见,避免使用前导或双下划线。
回答by IronMensan
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.
您是将错误消息复制到问题中还是重新输入?因为错误 14 的 'Class' 带有大写字母 C,这几乎肯定是不对的。
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
此外,您应该在头文件中使用尽可能少的包含指令。例如,GameScreen 不使用 PlayerEntity,因此您可以删除该包含,而 BaseEntity 仅通过指针使用,因此您可以替换
#include "BaseEntity.h"
with a forward declaration
有预先声明
class BaseEntity;