簡體   English   中英

編譯器錯誤C2653:不是類或命名空間名稱

[英]Compiler error C2653: not a class or namespace name

所以我最近在使用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

整個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 ,因為它根本不需要; 並且不包括BaseScreen.hScreenSystem.h因為只有一個聲明class BaseScreen; 需要,而不是完整的類定義。

另外,請檢查您是否沒有重復的標頭保護。 其中一些與標題名稱不匹配(例如GHANDLER_HScreenSystem.h ),這使我認為它們可能是從其他標題中意外復制的。 最后,不要將_EntitySprite等保留名稱用於自己的符號; 為簡單起見,避免引導或雙下划線。

您是否將錯誤消息復制到您的問題中或者是否重新鍵入了它們? 因為錯誤14具有帶有大寫字母C的“類”,這幾乎肯定是不正確的。

此外,您應該盡可能少地使用頭文件中的include指令。 例如,GameScreen不使用PlayerEntity,因此您可以刪除包含,而BaseEntity僅用於指針,因此您可以替換

#include "BaseEntity.h"

提出前瞻性聲明

class BaseEntity;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM