![](/img/trans.png)
[英]I am getting an undeclared identifier error but I have included the header file?
[英]Why am I getting an “use of undefined type” error in my code, when I have the header for it included?
我正在學習C ++,並嘗試使用Direct3D編寫一個簡單的游戲。 在我的游戲項目中,我在整個游戲中使用一個名為GameEngine
名稱空間。 我的游戲邏輯包含在一個名為Game
的主類中。 該Game
類將具有諸如輸入管理器和對象管理器之類的成員變量。 這些將是私有成員,但是我在Game
類上有一個公共函數,該函數返回一個指向InputManager
類的指針。 這樣,我可以告訴InputManager
在程序的主PeekMessage
循環內處理窗口消息。
這是我的主要消息循環...
// 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
時出現錯誤。 它說我正在使用一個未定義的類型InputManager
。 GetInputManager
函數返回一個指向InputManager
的指針。 在主消息循環所在的Main.cpp
文件的頂部,我包含了包含InputManager
定義的標頭,即InputManager.h
。 因此,我不太確定為什么會說這是未定義的類型。
有人知道發生此錯誤嗎? 我試圖在這些頭文件中的一些中第一次使用前向聲明,並且我想也許與這些有關?
我將整個文件按文件組織了代碼粘貼到Github上: https : //gist.github.com/ryancole/5936795#file-main-cpp-L27
文件已正確命名,並且錯誤行在粘貼底部附近突出顯示。
Game.h
forward 在全局名稱空間中聲明一個class InputManager
,但真正的InputManager
類在名稱空間GameEngine
。
由於這兩個聲明位於不同的名稱空間中,因此它們彼此獨立,並且全局名稱空間中的InputManger
保留為不完整的類型。 要解決此問題,請將前向聲明移到名稱空間中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.