簡體   English   中英

當包含標題時,為什么在代碼中出現“使用未定義類型”錯誤?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM