如何创建静态成员函数的线程例程
class Blah
{
static void WINAPI Start();
};
// ..
// ...
// ....
hThread = (HANDLE)_beginthreadex(NULL, 0, CBlah::Start, NULL, NULL, NULL);
这给了我以下错误:
***error C2664: '_beginthreadex' : cannot convert parameter 3 from 'void (void)' to 'unsigned int (__stdcall *)(void *)'***
我做错了什么?
最佳答案
有时,阅读您遇到的错误很有用。
cannot convert parameter 3 from 'void (void)' to 'unsigned int (__stdcall *)(void *)'
让我们看看它说了什么。对于第三个参数,您给它一个带有签名 void(void)
的函数,即一个不接受任何参数且不返回任何内容的函数。它无法将其转换为 unsigned int (__stdcall *)(void *)
,这正是 _beginthreadex
期望:
它需要一个函数:
- 返回一个
unsigned int
: - 使用
stdcall
调用约定 - 采用
void*
参数。
所以我的建议是“给它一个带有它要求的签名的函数”。
class Blah
{
static unsigned int __stdcall Start(void*);
};
关于c++ - _beginthreadex 静态成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1259815/