2016-04-08 61 views
0

正手我想提一提我是相当新的C++编程,并且我使用Ogre3D作为框架(用于学校项目的原因)。 我有一个类玩家它继承自GameObject类。当试图建立我面临着以下错误的项目:未定义的基类,虽然包括目前

Error C2504 'GameObject' : base class undefined - player.h (9)

这将意味着游戏对象类是球员类的头文件中未定义。不过,实际上我已经在Player中包含了GameObject头文件(参见下面的代码)。我知道通知包括在代码中发生。但是,如果我离开了这包括我得到它我不知道他们是如何或为何出现不同的错误的完整列表:

compiler errors

现在我已经难倒这个问题几天并且还没有在互联网上找到任何解决方案(CPlusPlus文章,我主要咨询过:http://www.cplusplus.com/forum/articles/10627/)。

下面列出的头文件的源文件只包含它们各自的头文件。

Player.h

#pragma once 

#ifndef __Player_h_ 
#define __Player_h_ 

#include "GameObject.h" 

class Player : public GameObject { 
    // ... Player class interface 
}; 
#endif 

GameObject.h

#pragma once 

#ifndef __GameObject_h_ 
#define __GameObject_h_ 

#include "GameManager.h" 

// Forward declarations 
class GameManager; 

class GameObject { 
// ... GameObject class interface 
}; 
#endinf 

游戏对象标题包括游戏管理如可以看到的。

GameManager.h

#pragma once 

// Include guard 
#ifndef __GameManager_h_ 
#define __GameManager_h_ 

// Includes from project 
#include "Main.h" 
#include "Constants.h" 
#include "GameObject.h" // mentioned circular includes 
#include "Player.h" // " 

// Includes from system libraries 
#include <vector> 

// Forward declarations 
class GameObject; 

class GameManager { 
// ... GameManager interface 
}; 
#endif 

最糟糕的还有主类的头文件如下所示:

Main.h

// Include guard 
#ifndef __Main_h_ 
#define __Main_h_ 

// Includes from Ogre framework 
#include "Ogre.h" 
using namespace Ogre; 

// Includes from projet headers 
#include "BaseApplication.h" 
#include "GameManager.h" 

// forward declarations 
class GameManager; 

class Main : public BaseApplication 
{ 
// ... Main interface 
}; 
#endif 

随着所有关于这个主题的文章以及其他与我相同的错误的人都会读到能够弄清楚但还无济于事。我希望有人能花时间帮助我,并指出任何错误的代码或惯例。

+0

你的类定义之后有';'吗?他们缺失 – vu1p3n0x

+0

我的不好,是的类定义以分号结尾。我将相应地编辑主帖。 – Stephan

+1

包括类定义和前向声明该类都没有意义。如果前向声明足够,请删除包含。如果您需要类定义,请删除声明。 – molbdnilo

回答

0

我认为解决这个问题最简单的方法是改变你的模型以包含头文件。文件A.h应该只包含B.h如果B.h定义了一个在A.h.中直接使用的符号。在头文件中放入一个using子句通常也是一个坏主意 - 让客户端代码的程序员做出决定。除非它们是绝对必要的,否则将前面的类声明放在前面在#include“GameManager.h”之后不需要类GameManager。我怀疑代码有其他问题,但类的前向声明隐藏了这个问题。如果更改包含不能解决问题,请从包含“最简单”标题(不依赖于其他标题)的单个.cpp文件开始,然后构建到完整的包含集合。

+0

尽可能避免在头文件中包含语句。改用前向声明。 – Ceros

+0

与您的方法清除所有包括和声明,并逐步包括所有缺少的参考。最终它归结为GameManager包括在Player和GameObject中成为问题。由于在这些代码中没有实际用途,我将它们移除并且可以构建项目。 做了一个备份,我会进入制作更好的标题制服。谢谢! – Stephan