2016-04-08 22 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

相關問題