正手我想提一提我是相当新的C++编程,并且我使用Ogre3D作为框架(用于学校项目的原因)。 我有一个类玩家它继承自GameObject类。当试图建立我面临着以下错误的项目:未定义的基类,虽然包括目前
Error C2504 'GameObject' : base class undefined - player.h (9)
这将意味着游戏对象类是球员类的头文件中未定义。不过,实际上我已经在Player中包含了GameObject头文件(参见下面的代码)。我知道通知包括在代码中发生。但是,如果我离开了这包括我得到它我不知道他们是如何或为何出现不同的错误的完整列表:
现在我已经难倒这个问题几天并且还没有在互联网上找到任何解决方案(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
随着所有关于这个主题的文章以及其他与我相同的错误的人都会读到能够弄清楚但还无济于事。我希望有人能花时间帮助我,并指出任何错误的代码或惯例。
你的类定义之后有';'吗?他们缺失 – vu1p3n0x
我的不好,是的类定义以分号结尾。我将相应地编辑主帖。 – Stephan
包括类定义和前向声明该类都没有意义。如果前向声明足够,请删除包含。如果您需要类定义,请删除声明。 – molbdnilo