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

StackOverflow https://stackoverflow.com/questions/17496200

  •  02-06-2022
  •  | 
  •  

Domanda

I'm learning C++ and am trying to write a simple game, using Direct3D. In my game project, I use a single namespace throughout my game, named GameEngine. My game logic is contained within a main class, named Game. That Game class will have member variables for things like the input manager and object manager. These will be private members, but I have a public function on my Game class that returns a pointer to the InputManager class. That way, I can tell the InputManager to handle window messages inside of my main PeekMessage loop of the program.

Here's my main message loop ...

// 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();

I'm getting an error when I call GetInputManager, though. It says I'm using an undefined type InputManager. The GetInputManager function returns a pointer to an InputManager. At the top of my Main.cpp file, in which this main message loop resides, I include the header that contains the definition of InputManager, which is InputManager.h. So, I'm not quite sure why it says this is an undefined type.

Does anybody know what this error is occurring? I'm trying to use forward declarations for the first time in some of these header files, and I'm thinking maybe it has to do with those?

I pasted the whole code, organized by file, on Github here: https://gist.github.com/ryancole/5936795#file-main-cpp-L27

The files are properly named and the error line is highlighted near the bottom of the paste.

È stato utile?

Soluzione

Game.h forward declares a class InputManager in the global namespace, but the real InputManager class is in namespace GameEngine.

Since the two declarations are in different namespaces they are independent of each other and the InputManger in the global namespace stays a incomplete type. To fix the problem, move the forward declaration into the namespace.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top