首页 > 解决方案 > 为什么 c++ DLL 中的多线程在构建时会出现错误 C3867?

问题描述

我正在使用 VS2019

试图调用 DLL 中的线程。使用分离同时运行两个可执行文件

当我运行一个普通的 c++ 程序时,以下线程工作

我收到错误

错误 C3867 'myClass::runexeone':非标准语法;使用 '&' 创建指向成员 myGateway C:\Users\user\Downloads\Demo\myGateway\myplugin.cpp 的指针 21

插件头

#include <windows.h>
#include <iostream>
#include <thread> 


#define MYPLUGIN_EXPORT __declspec(dllexport)

extern "C"
{
    MYPLUGIN_EXPORT void WINAPI OnStart();
}

插件.cpp

#include "plugin.h"
using namespace std;

class myClass
{
public:
    myClass()
    {

    }
    ~myClass()
    {

    }
    void onStart()
    {
        std::thread(runexeone).detach();
        std::thread(runexetwo).detach();
    }

    void runexeone()
    {
        int exerunpne = system("cmd /C \"%MY_ROOT%\\bin\\Mytest.exe\" -ORBEndpoint iiop://localhost:12345 -d");
    }


    void runexetwo()
    {
        int exeruntwo = system("cmd /C \"%MY_ROOT%\\bin\\Mytest_2.exe\" -ORBEndpoint iiop://localhost:12345 -d");
    }

};

myClass& getmyclass()
{
    static myClass myclass;
    return myclass;
}


MYPLUGIN_EXPORT void WINAPI OnStart()
{
    getmyClass().onStart();
}

标签: c++multithreadingdllexecutablestdthread

解决方案


问题是这runexeone是一个成员函数的非限定名称,并且std::thread需要一些可执行的东西。runexeone不是。VC++ 试图从上下文中猜测您的意思,但建议还不够。即使你已经写了&myClass::runexeone,它仍然不会起作用,因为myClass::runexeone还需要一个this指针。您可以通过 make 解决后一个问题static

当只有一个问题时,编译器建议效果最好。


推荐阅读