jeudi 25 avril 2019

Unresolved external symbols when one completely basic class is a member of another completely basic class

unresolved external symbols...

I was starting a new project in C++ and ran into a problem of unresolved external symbols while building with Microsoft Visual C++. After wasting 30 minutes of my time trying to find out why, I did not find any reason whatsoever. I boiled the problem down to the most basic thing possible, as follows:

Class GameWindow, with constructor and destructor, is used as a member field in Class Game, with constructor and destructor. Both are declared correctly in their respective header files, and Game correctly includes GameWindow as should. Both are also implemented correctly. Though when building (Microsoft C++ compiler and linker from Visual Studio 2017) it starts complaining about unresolved external symbols:

Error LNK1120 2 unresolved externals

Error LNK2019 unresolved external symbol "public: __cdecl GameWindow::GameWindow(void)" (??0GameWindow@@QEAA@XZ) referenced in function "public: __cdecl Game::Game(void)" (??0Game@@QEAA@XZ)

Error LNK2019 unresolved external symbol "public: __cdecl GameWindow::~GameWindow(void)" (??1GameWindow@@QEAA@XZ) referenced in function "public: __cdecl Game::~Game(void)" (??1Game@@QEAA@XZ)

To rule out magic dragons and other shenanigans, I started a new project from scratch, typed the same 'code', and the same problem occurred. I have no clue why and I'm about to pull out my hair; or that what's left of it after about 35 years of programming...

GameWindow.h

#pragma once

class GameWindow
{
public:

    GameWindow();
    ~GameWindow();
};

GameWindow.cpp

#include "GameWindow.h"

GameWindow::GameWindow()
{
}

GameWindow::~GameWindow()
{
}

Game.h

#pragma once

#include "GameWindow.h"

class Game
{
public:

    Game();
    ~Game();

private:

    GameWindow window;
};

Game.cpp

#include "Game.h"

Game::Game()
{
}

Game::~Game()
{
}

main.cpp

#include "Game.h"

int main(int argc, char* argv[])
{
    Game game;
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire