我有一个用于项目的非常简单的线程类。我刚刚开始解决问题,但由于LNK2019错误而无法解决,我无法解决。我已将问题缩小为一行。也许有人可以帮助指导我解决该问题的方法。

以下是我正在上的课:

#ifndef __THREADING_H
#define __THREADING_H

#include <Windows.h>

class Threading {

public:

    virtual void run() = 0;

    void start();
    void stop();

    bool isStopped();
    void cleanup();

private:
    bool stopped;
    HANDLE reference;
    static DWORD WINAPI start_helperfunction(LPVOID ptr);

};

#endif // __THREADING_H


我得到错误的行是start_helperfunction的第二行,下面是Threading::start_helperfunction

#include "Threading.h"

void Threading::start()
{
    stopped = false;
    reference = CreateThread(NULL, 0, Threading::start_helperfunction, this, NULL, NULL);
}


最后,我收到的错误消息是:

error LNK2019: unresolved external symbol "private: static unsigned long __stdcall Threading::start_helperfunction(void *)" (?start_helperfunction@Threading@@CGKPAX@Z) referenced in function "public: void __thiscall Threading::start(void)" (?start@Threading@@QAEXXZ)


我不确定自己做错了什么或尝试什么。我敢肯定这是一个简单的解决方法。我不是C ++经验最丰富的人。

最佳答案

您没有实现start_helperfunction,因此链接程序找不到它。您实际上需要使用该名称编写一个静态成员函数。最简单的一个可能是这样的:

DWORD WINAPI Threading::start_helperfunction(LPVOID ptr)
{
    return 0;
}

关于c++ - 如何修复LNK2019无法解析的外部符号,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28203427/

10-16 06:20