【问题标题】:Visual C++ Studio 2010 shows build errors for no reasonVisual C++ Studio 2010 无故显示构建错误
【发布时间】:2014-07-05 16:41:42
【问题描述】:

显然我不能在另一个类的公共部分声明一个类的实例。

我有两个类:Game 和 ScreenManager。如果我从 Game.h 中删除以下行,一切都会成功编译:

ScreenManager screenManager;

如果我不这样做,我会得到错误。这些是我在构建时收到的错误消息:

1>c:\users\dziku\documents\visual studio 2010\projects\test allegro game\test allegro game\game.h(29): error C2146: syntax error : missing ';' before identifier 'screenManager'
1>c:\users\dziku\documents\visual studio 2010\projects\test allegro game\test allegro game\game.h(29): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\dziku\documents\visual studio 2010\projects\test allegro game\test allegro game\game.h(29): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

游戏.h:

#pragma once

#include"ScreenManager.h"
#include<allegro5\allegro.h>
#include<allegro5\allegro_image.h>
#include<allegro5\allegro_font.h>
#include<allegro5\allegro_ttf.h>
#include<allegro5\allegro_primitives.h>


class Game
{
public:

    static const int WINDOW_WIDTH=800;
    static const int WINDOW_HEIGHT=640;

    static const int FPS=60;
    float FRAME_INTERVAL;

    bool isExiting;

    float currentTime,prevTime,lag;

    ALLEGRO_DISPLAY* display;
    ALLEGRO_EVENT_QUEUE* eventQueue;
    ALLEGRO_EVENT ev;

    ScreenManager screenManager;

    Game(void);
    ~Game(void);

    static Game &getInstance();

    void initialize();
    void gameLoop();
    void cleanup();
    void update(ALLEGRO_EVENT &ev);
    void render(ALLEGRO_DISPLAY* display);
};

还有 ScreenManager.h:

#pragma once

#include"Game.h"
#include<allegro5\allegro.h>
#include<vector>
#include<map>

class ScreenManager
{
public:
    ScreenManager(void);
    ~ScreenManager(void);

    void initialize();
    void update(ALLEGRO_EVENT &ev);
    void render(ALLEGRO_DISPLAY* display);
    void unloadContent();

};

我真的不明白发生了什么,从昨天开始,我一直在遇到类似的错误,在其他项目中也是如此。我一定做错了什么,但我不知道所以任何帮助将不胜感激。

【问题讨论】:

  • 递归包含 - 使用前向类声明修复
  • 这些标头之间存在循环依赖关系。 Game.h 包含 ScreenManager.h,ScreenManager.h 包含 Game.h。有人会输。嗯,你。这是一个设计缺陷。您需要重新组织这些文件的内容。如有必要,可能使用前向声明,期望您可能必须使用 ScreenManager*。

标签: c++ visual-studio-2010 class compiler-errors


【解决方案1】:

您能解释一下为什么标题"ScreenManager.h" 包含标题"Game.h"

ScreenManager.h:

#pragma once

#include"Game.h"

如果 ScreenManager 类成员函数使用了 Game 类成员函数的一些数据成员,那么你应该将类定义和 uts 成员函数的定义分开。只在类定义中声明成员函数,并将它们的实现放在某个单独的模块中。如果您愿意,可以将它们内联,这样指定函数说明符内联。

或者您可以在头文件 ScreenManager 中转发声明类 Game 为

class Game;

并在单独的模块中再次定义类 ScreeManager 的成员函数。

【讨论】:

  • 嗯,我刚刚发现这就是我的代码无法编译的原因。我包含“Game.h”以从 Game 类外部访问“isExiting”布尔变量,但不知道该怎么做。
  • @user264 您可以在第三个标头中声明所有公共变量,并将此标头包含在其他两个中。
  • @user264 我已经看到这个变量是 Game 类的数据成员。如果它在类 ScreenManager 的某些方法中使用,则应将类定义与其成员函数的定义分开。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-11-04
  • 2011-11-14
  • 2011-05-28
  • 1970-01-01
  • 2021-03-25
  • 1970-01-01
  • 2012-06-15
相关资源
最近更新 更多