未定义的基类,但包括当前

时间:2016-04-08 17:19:04

标签: c++ inheritance include undefined header-files

正手我想提一下我对C ++编程相当新,并且我使用Ogre3D作为框架(出于学校项目的原因)。 我有一个继承自 GameObject 类的 Player 类。在尝试构建项目时,我遇到了以下错误:

  

错误C2504'GameObject':基类未定义 - player.h(9)

这意味着GameObject类在播放器类的头文件中是未定义的。但实际上我将GameObject头文件包含在Player的头文件中(参见下面的代码)。我知道循环包括在代码中发生。但是,如果我遗漏这些内容,我会得到一份不同错误的完整列表,我不确定它们是如何发生的或原因:

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

GameObject标题包含 GameManager ,可以看到。

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类,其头文件如下所示:

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

随着我对这个主题所做的所有阅读以及其他具有相同错误的人,我想我能够弄清楚但却无济于事。我希望有人可以花时间帮助我,并指出任何错误的代码或约定。

1 个答案:

答案 0 :(得分:0)

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

相关问题