2013-07-05 15 views
1

我正在學習C++,並試圖使用Direct3D編寫一個簡單的遊戲。在我的遊戲項目中,我在整個遊戲中使用了一個命名空間,名稱爲GameEngine。我的遊戲邏輯包含在一個名爲Game的主類中。 Game類將具有諸如輸入管理器和對象管理器之類的成員變量。這些將是私人成員,但我在我的Game類上有公共職能,該類返回指向InputManager類的指針。這樣,我可以告訴InputManager處理程序的主循環中的窗口消息。爲什麼我在代碼中使用「未定義類型」錯誤時,包含頭文件?

這裏是我的主消息循環...

// instanciate the game 
GameEngine::Game game(windowRectangle.bottom, windowRectangle.right); 

// initialize D3D 
game.InitializeDirect3D(hWnd); 
game.InitializePipeline(); 

// main game loop 
while (true) 
{ 
    // check for received event messages 
    if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
    { 
     bool handled = false; 

     if (msg.message >= WM_MOUSEFIRST && msg.message <= WM_MOUSELAST) 
     { 
      handled = game.GetInputManager()->HandleMouseInput(&msg); 
     } 
     else if (msg.message >= WM_KEYFIRST && msg.message <= WM_KEYLAST) 
     { 
      handled = game.GetInputManager()->HandledKeyboardInput(&msg); 
     } 
     else if (msg.message == WM_QUIT) 
     { 
      break; 
     } 

     if (handled == false) 
     { 
      TranslateMessage(&msg); 
      DispatchMessageA(&msg); 
     } 
    } 

    // render the current frame 
    game.RenderFrame(); 
} 

// tear down D3D 
game.CleanDirect3D(); 

我得到一個錯誤,當我打電話GetInputManager,雖然。它說我正在使用一個不明確的類型InputManagerGetInputManager函數返回一個指向InputManager的指針。在我的Main.cpp文件的頂部,其中包含此主消息循環所在的文件,其中包含標頭,其中包含InputManager的定義,即InputManager.h。所以,我不太確定它爲什麼說這是一個未定義的類型。

有沒有人知道這個錯誤發生了什麼?我試圖在這些頭文件中首次使用前向聲明,我想也許它與這些有關?

我粘貼整個代碼,按文件組織,在Github這裏:https://gist.github.com/ryancole/5936795#file-main-cpp-L27

這些文件都是正確的和錯誤行被突出顯示附近的糊底。

謝謝!

+0

它在GameEngine命名空間中,你有文件中使用GameEngine嗎? – Alex1985

+0

通告包括 –

+0

您的包含警衛使用[保留標識符](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier)。 – chris

回答

2

Game.h向前聲明在全局命名空間一個class InputManager,但真正InputManager類是在命名空間GameEngine

由於兩個聲明位於不同的名稱空間中,因此它們彼此獨立,並且全局名稱空間中的InputManger保持不完整類型。要解決該問題,請將前向聲明移入命名空間。

+0

謝謝。這固定了它。我沒有完全意識到如何使用前向聲明,但現在我更好地理解它們。 – Ryan

相關問題