当我包含它的标头时,为什么我的代码中会出现"use of undefined type"错误?

Why am I getting an "use of undefined type" error in my code, when I have the header for it included?

本文关键字:use of 错误 type undefined 代码 包含它 我的 为什么      更新时间:2023-10-16

我正在学习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。CCD_ 10函数返回指向CCD_ 11的指针。在这个主消息循环所在的Main.cpp文件的顶部,我包含了包含InputManager定义的头,即InputManager.h。所以,我不太清楚为什么它说这是一个未定义的类型。

有人知道这个错误是怎么发生的吗?我第一次尝试在这些头文件中使用正向声明,我想这可能与这些有关?

我把整个代码按文件组织粘贴到Github上:https://gist.github.com/ryancole/5936795#file-主-cpp-L27

文件的名称正确,错误行在粘贴底部附近突出显示。

Game.h正向声明全局命名空间中的class InputManager,但真正的InputManager类在命名空间GameEngine中。

由于这两个声明位于不同的命名空间中,它们彼此独立,并且全局命名空间中的InputManger保持为不完整类型。若要解决此问题,请将forward声明移到命名空间中。