类错误导致几百个错误

Error with class causing a few hundred errors

本文关键字:错误 百个      更新时间:2023-10-16

我的类标题是

#ifndef _CENGINE_H
#define _CENGINE_H
#include "SFMLGraphics.hpp"
#include "CTextureManager.h"
#include "CTile.h"
class CEngine
{
private:
    //Create instance of CTextureManager
    CTextureManager textureManager;
    //Load textures
    void LoadTextures();
    //New tile
    CTile* testTile;
    bool Running; //Is running?
    sf::RenderWindow* window; //Create render window
public:
    CEngine(); //Constructor
    int Execute(); //Execute
    bool OnInit(); //On intialization
    void GameLoop(); //Main game loop
    void Render(); //Render function
    void Update(); //Update
};
#endif

现在它给我的 3 个错误是:

cengine.h(8):错误 C2236:意外的"类"CEngine"。你忘了";"吗?

cengine.h(8): 错误 C2143: 语法错误: 在"{"之前缺少";"

cengine.h(8): 错误 C2447: '{' : 缺少函数头(旧式正式列表?

我知道错误很明显,但我看不出类有问题。我可能真的很愚蠢,因为我累了。

这似乎是一个循环包含问题。CTextureManager.hCTile.h是否包括彼此或CEngine.h

若要解决此问题,请尽可能使用前向声明。例如,您的类不需要包含CTile.h - 它可能看起来像:

#ifndef CENGINE_H
#define CENGINE_H
#include "SFMLGraphics.hpp"
#include "CTextureManager.h"

class CTile;    //forward declaration instead of include
class CEngine
{
private:
    //Create instance of CTextureManager
    CTextureManager textureManager;
    //Load textures
    void LoadTextures();
    //New tile
    CTile* testTile;
    bool Running; //Is running?
    sf::RenderWindow* window; //Create render window
public:
    CEngine(); //Constructor
    int Execute(); //Execute
    bool OnInit(); //On intialization
    void GameLoop(); //Main game loop
    void Render(); //Render function
    void Update(); //Update
};
#endif

其他 2 个标头类似。

另外,_CENGINE_H不是有效的标识符 - 请注意我如何将其重命名为CENGINE_H