【问题标题】:Error with class causing a few hundred errors类错误导致数百个错误
【发布时间】:2012-11-21 06:27:42
【问题描述】:

我的班级标题是

#ifndef _CENGINE_H
#define _CENGINE_H
#include "SFML\Graphics.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): error C2447: '{' : 缺少函数头(旧式正式列表?)

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

【问题讨论】:

  • 查看您之前包含的头文件,您可能在其中一个文件中忘记了分号。
  • 问题很可能出在您未显示的代码中。最有可能的罪魁祸首是 CTile 或 CTextureManager。检查这些标题是否缺少;
  • 那实际上会产生那么大的影响吗?我以为它只会给我一个错误,告诉我我忘记了。
  • 哇,果然如此。在我看来,所有错误都来自 CEngine 标头,因为它们需要对其进行初始化。我实际上忘记了 CTile 标题的最后一个方括号上的分号。谢谢!
  • 请记住,预处理器只是将 #include 指令替换为包含文件的实际内容,因此从实际编译器来看,它是一个单独的文件,它看到一个右大括号后跟关键字 @987654324 @ 之间没有分号。

标签: c++ class sfml


【解决方案1】:

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

要解决此问题,请尽可能使用前向声明。例如,您的课程不需要包含 CTile.h - 它可以如下所示:

#ifndef CENGINE_H
#define CENGINE_H
#include "SFML\Graphics.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

【讨论】:

  • 他不能转发声明 CTextureManager。
  • @the.malkolm 我也没有。不过他可以转发声明 CTile。
  • _CENGINE_H 不是有效标识符?怎么会?这不是标准用法,但他可以完美地使用它。
  • @revani 标准禁止标识符以下划线开头,后跟大写字母。
  • @user1816388 无论它是否适用于您的编译器,它都是非法的。您还应该接受关于前向声明而不是完全包含的建议。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-16
  • 2016-11-12
  • 2018-01-13
  • 1970-01-01
  • 2014-07-01
  • 2022-08-11
  • 1970-01-01
相关资源
最近更新 更多