2014-07-05 61 views
0

显然,我无法在另一个类的公共部分中声明一个类的实例。Visual C++ Studio 2010无故显示构建错误

我有两个类:Game和ScreenManager。

ScreenManager screenManager; 

如果我不这样做,我得到的错误:如果我从Game.h删除以下行一切编译成功。这些都是错误消息我得到的构建:

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 

Game.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(); 

}; 

我真的不明白是怎么回事,自昨天以来,我一直在类似的错误,也在其他项目。有一件事我必须做错,但我不知道如何帮助将不胜感激。

+1

递归包括 - 使用正向类声明来修复 – sp2danny

+1

这些标头之间有循环依赖关系。 Game.h包含ScreenManager.h,ScreenManager.h包含Game.h.有人会输。好吧,你。这是一个设计缺陷。您需要重新组织这些文件的内容。如果需要,可能需要使用前向声明,并期望您可能必须使用ScreenManager *。 –

回答

1

你能解释为什么标题"ScreenManager.h"包含heafder "Game.h"

ScreenManager.h:

#pragma once 

#include"Game.h" 

如果屏幕管理类的成员函数使用的游戏类成员函数的一些数据成员,那么你应该分开类的定义和UTS成员函数的定义。只在类定义中声明成员函数及其在某个独立模块中的实现位置。如果您想要内联指定函数说明符,可以使它们成为内联。

或者VOU可以转发在标题屏幕管理声明类游戏作为

class Game; 

,并再次确定在一个单独的模块类ScreeManager的成员函数。

+0

嗯,我刚刚发现这是我的代码无法编译的原因。 我包含'Game.h'以从外部Game类访问'isExiting'布尔变量,但没有其他想法如何去做。 – user2648421

+0

@ user264您可以在第三个标头中声明所有通用变量,并在另外两个标头中包含此标头。 –

+0

@ user264我已经看到这个变量是类Game的数据成员。如果在类ScreenManager的某些方法中使用它,则应该分离类定义及其成员函数的定义。 –