문제

I began C++ quite recently and I obviously have the famous LNK2019 issue. I roamed a few hours on google but nothing solved my problem. My project is half way coded, since I separate the view and the model. I work with Visual Studio 2010.

Here is the class whose function is not retrieved:

Display.h:

#ifndef DEF_DISPLAY
#define DEF_DISPLAY
#include <Windows.h>
#include <exception>

class Display{

public:
    HWND mainWindow, gameWindow;
    WNDCLASS mainClass, gameClass;

public:
    Display();
    static LRESULT CALLBACK mainWindowProc(HWND mainWin, UINT message, WPARAM wParam, LPARAM lParam);
    static LRESULT CALLBACK gameWindowProc(HWND gameWin, UINT message, WPARAM wParam, LPARAM lParam);
    **int run();** // This function is not retrieved by the linker.
};

#endif

And here is the Display.cpp:

#include "Display.h"

HINSTANCE instanceMain = 0, instanceGame = 0;

Display::Display(){...}

LRESULT CALLBACK Display::mainWindowProc(HWND mainWin, UINT message, WPARAM wParam, LPARAM lParam){...}

LRESULT CALLBACK Display::gameWindowProc(HWND gameWin, UINT message, WPARAM wParam, LPARAM lParam){...}

int run(){
    MSG message;

    while(GetMessage(&message, 0, 0, 0)){
    TranslateMessage(&message);
    DispatchMessage(&message);
    }
    return message.wParam;
}

And finally here is my main.cpp:

#include "Display.h"

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow){

    Display game;

    return game.run();
}

I did not finished to code my project because I found out that issue when building it:

1>  All outputs are up-to-date.
1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall Display::run(void)" (?run@Display@@QAEHXZ) referenced in function _WinMain@16
1>C:\Users\glembalis\Documents\Visual Studio 2010\Projects\pendu\Debug\pendu.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.

I don't know where the error can occur.

  1. Display.h and Display.cpp are included in the project
  2. The option in Project > Properties > Linker > System > SubSystem is "Windows"
  3. I do not use external libraries (only Windows.h and exception)

The compiler seems to work well. I don't really care if the program works properly, I would correct it later. For now, this linker issue is my major concern! I guess it is just a tiny little stupid mistake, but I cannot find it out!

Thanks to everyone for your time and attention, looking forward to have your answers! Last, I apologise but english is not my native language and I may have written some mistakes.

Have a nice day!

NoobFeeder

도움이 되었습니까?

해결책

Your definition (implementation) has the wrong signature.

It should look like this:

 int Display::run(){

This tells the compiler that your run is the one that's a member of your Display class.

Currently you have implemented a free function called run.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top