所以我最近在使用 Visual C++ 2012 时遇到了这个非常令人沮丧的问题。直到几个小时前,我还在编写代码,一切都按预期工作,直到我决定优化一些东西并删除了一些类.我修复了所有因此而弹出的错误,例如false 包含等。不幸的是,在此之后 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
Whole 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
最佳答案
您的 header 中有循环依赖。 BaseEntity.h
包含 Input.h
,其中包含 ScreenSystem.h
,其中包含 GameScreen.h
,在转而重新包含 BaseEntity.h
。这会导致类名在声明之前出现,导致编译失败。
为避免这种情况,请不要不必要地包含标题。例如,不要包含 BaseEntity.h
中的 Input.h
,因为它根本不需要;并且不要包含 ScreenSystem.h
中的 BaseScreen.h
,因为只需要声明 class BaseScreen;
,而不需要完整的类定义。
另外,请检查您没有重复的 header 保护。其中一些与标题名称不匹配(例如 GHANDLER_H
for ScreenSystem.h
),这让我认为它们可能是从其他标题中意外复制的。最后,不要为自己的符号使用像 _EntitySprite
这样的保留名称;为简单起见,请避免使用前导或双下划线。
关于c++ - 编译器错误 C2653 : not a class or namespace name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15740952/