在VS2010中链接期间出现LNK2019错误

时间:2013-08-21 12:25:21

标签: c++ winapi lnk2019

我最近开始使用C ++,显然我有着名的LNK2019问题。我在谷歌上漫游几个小时,但没有解决我的问题。 我的项目是半路编码,因为我将视图和模型分开。 我使用Visual Studio 2010。

这是未检索其功能的类:

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

这是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;
}

最后这是我的main.cpp:

#include "Display.h"

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

    Display game;

    return game.run();
}

我没有完成对我的项目进行编码,因为我在构建项目时发现了这个问题:

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.

我不知道错误发生在哪里。

  1. Display.h和Display.cpp包含在项目中
  2. Project&gt;中的选项属性&gt;链接器&gt;系统&gt; SubSystem是“Windows”
  3. 我不使用外部库(仅限Windows.h和例外)
  4. 编译器似乎运行良好。我真的不在乎程序是否正常工作,我稍后会更正。目前,这个链接器问题是我主要担心的问题!我想这只是一个小小的愚蠢的错误,但我找不到它!

    感谢大家的时间和关注,期待您的回答!最后,我道歉但英语不是我的母语,我可能写了一些错误。

    度过美好的一天!

    NoobFeeder

1 个答案:

答案 0 :(得分:2)

您的定义(实施)签名错误。

它应该是这样的:

 int Display::run(){

这告诉编译器您的runDisplay类的成员。

目前,您已实施了名为run的免费功能。