c – 编译器错误C2653:不是类或命名空间名称
所以我最近在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 Whole 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文件: #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文件: #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文件: #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 解决方法
你的标题中有循环依赖. BaseEntity.h包括Input.h,其中包括ScreenSystem.h,其中包括GameScreen.h,后者又重新包含BaseEntity.h.这导致类名在它们被声明之前出现,导致编译失败.
为避免这种情况,请勿不必要地加入标题.例如,不要在BaseEntity.h中包含Input.h,因为它根本不需要;并且不包括来自ScreenSystem.h的BaseScreen.h,因为只有一个声明类BaseScreen;是必需的,而不是完整的类定义. 另外,检查你没有重复的头卫.其中一些与标题名称不匹配(例如ScreenSystem.h的GHANDLER_H),这使我认为它们可能已经从其他标题中被意外复制.最后,不要为你自己的符号使用_EntitySprite这样的保留名称;为了简单起见,避免前导或下划线. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |