一个 friend 给我发了他的线程类(class)。
现在我只想运行一个监听器,但线程不想接受该函数。
我想执行函数(在 static class Networks
中定义)THREAD listen(void* args)
。 THREAD
是一个#define THREAD unsigned __stdcall
Networks::init() {
listenerth = thread();
listenerth.run(listen, NULL, "listener");
}
在 class thread
中,他将 run
定义为 void run( THREAD func(void*), void* args, const char* pname);
我怎样才能让它在另一个线程中运行?
[编辑] 错误信息: main.cpp(19): error C3867: 'Networks::listen': 函数调用缺少参数列表;使用 '&Networks::listen' 创建指向成员的指针
但是当我将鼠标移到错误位置(符号监听)时,它会在工具提示中显示(是的,MS VC++):
unsigned int __stdcall Networks::listen(void* data)
Error: argument of type "unsigned int (__stdcall Networks::*)(void *data)" is incompatible with parameter of type "unsigned int (__stdcall )(void)"
最佳答案
正如其他人所说,您不能在这里使用非静态成员函数,因为它需要一个普通的函数指针。如果你需要调用一个非静态成员(因为它需要访问类中的状态),那么你可以使用 args
参数通过静态“蹦床”函数调用它,就像这样:
unsigned listen() {/* do the interesting stuff */}
static THREAD call_listen(void* me)
{
return static_cast<Networks*>(me)->listen();
}
void init()
{
listener = thread();
listener.run(call_listen, this, "listener");
}
关于c++ - 函数不想接受我的论点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3126664/