1

First of all, let me tell you that I'm super noob with C++. So, sorry if i'm asking something stupid, but i'm completely stuck.

I'm trying to port a little game engine i wrote in AS3 to C++ and I have started with the core Game class. It has to store the levels, to update and manage them, so I think pointers is the way to go.

The header looks like this:

#pragma once
#include "Level.h"

class Game
{
public:
    Level *level;
    Level *nextLevel;
    Game(void);
    virtual ~Game(void);
    virtual void begin();
    virtual void onEveryFrame();
private:
    void changeLevel();
};

And the compiler throws me two errors for every pointer declaration :

Error 1 error C2143: syntax error : missing ';' before '*'

Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

So, what am I doing wrong ? The error description doesn't looks too descriptive to me.

(I'm using VS Express 2012)

EDIT :

Sorry guys! Looks like is a circular dependency problem. Nothing to do with the pointers actually.

The "Level.h" was something like this:

#pragma once
#include "Game.h"

class Level
{
public:
    Game *game;
    Level(void);
    virtual ~Level(void);
    virtual void begin();
    virtual void onEveryFrame();
};

Sorry guys, noob error. :(

4

3 回答 3

4
  1. Level.h绝对希望您声明类型Level(很可能是类),但它有一些问题阻止Level声明类型。检查它或在此处发布。
  2. 您的Game.h标题实际上不需要了解Level类型详细信息。如果您的Level.h标头应该声明Level为类,那么您就足以class Level;在标头中声明Game.h。这足以使用指针。当然,如果您尝试操作类型的对象,则Level需要查看其接口。所以你肯定需要Level.h包含在模块实现Game类中。

更新
我注意到您的编辑,循环依赖完全通过我在 [2] 中提到的声明来解决。此外,它们还降低了项目垃圾和构建复杂性。这是 C / C++ 中广泛使用的技术(在 C 中通常是 struct 或类似的东西)。这个想法是在不需要实际对象大小或详细界面的任何地方都声明“它是类型”。

于 2013-06-26T19:49:49.140 回答
2

你有一个循环依赖,所以当编译器还没有任何关于“关卡”或“游戏”类型的信息时,无论你包含两个类之一的哪个头文件都需要另一个的前向声明是。

解决方案是一个简单的前向声明。

#pragma once
#include "Level.h"

class Level;

class Game
{
public:
    Level *level;
    Level *nextLevel;
    Game(void);
    virtual ~Game(void);
    virtual void begin();
    virtual void onEveryFrame();
private:
    void changeLevel();
};

并在 Level 标头中进行 Game 的相互前向声明。

于 2013-06-26T19:57:02.673 回答
0

您的编译器无法识别 type Level。也许是包含/错字错误?

于 2013-06-26T18:44:24.733 回答