Visual c++ Studio 2010会无缘无故地显示构建错误

Visual C++ Studio 2010 shows build errors for no reason

本文关键字:显示 构建 错误 无缘无故 c++ Studio 2010 Visual      更新时间:2023-10-16

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

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

ScreenManager screenManager;

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

1>c:usersdzikudocumentsvisual studio 2010projectstest allegro gametest allegro gamegame.h(29): error C2146: syntax error : missing ';' before identifier 'screenManager'
1>c:usersdzikudocumentsvisual studio 2010projectstest allegro gametest allegro gamegame.h(29): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:usersdzikudocumentsvisual studio 2010projectstest allegro gametest allegro gamegame.h(29): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

Game.h:

#pragma once
#include"ScreenManager.h"
#include<allegro5allegro.h>
#include<allegro5allegro_image.h>
#include<allegro5allegro_font.h>
#include<allegro5allegro_ttf.h>
#include<allegro5allegro_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<allegro5allegro.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();
};

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

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

ScreenManager.h:

#pragma once
#include"Game.h"

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

或者你可以在屏幕管理器头文件中将Game类声明为

class Game;

和在单独的模块中定义ScreeManager类的成员函数