显然,我无法在另一个类的公共部分中声明一个类的实例。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();
};
我真的不明白是怎么回事,自昨天以来,我一直在类似的错误,也在其他项目。有一件事我必须做错,但我不知道如何帮助将不胜感激。
递归包括 - 使用正向类声明来修复 – sp2danny
这些标头之间有循环依赖关系。 Game.h包含ScreenManager.h,ScreenManager.h包含Game.h.有人会输。好吧,你。这是一个设计缺陷。您需要重新组织这些文件的内容。如果需要,可能需要使用前向声明,并期望您可能必须使用ScreenManager *。 –