我正在尝试在对象中创建线程,但是出现错误消息“&” :对绑定成员函数表达式的非法操作.通过阅读,我看到我必须使成员函数静态化,但是当我这样做时,我收到一条错误消息,说’.dac_ping’的左边必须有class / struct / union
这是我正在尝试的:
class Dac
{
private:
network_com com;
HANDLE ping_thread;
DWORD dping_thread;
static DWORD WINAPI ping_loop(void* param)
{
while ( com.dac_ping() == 0)
Sleep(900);
return 1; //since this is an infinite loop, if the loop breaks, it has failed
}
public:
Dac()
{
}
~Dac()
{
}
void find_dac()
{
com.find_dac();
com.print_dac_info();
}
void connect_and_keep_alive()
{
if (com.dac_connect() == 0)
ping_thread = CreateThread (NULL , 0, ping_loop, NULL, 0, &dping_thread);
}
};
解决方法:
静态函数未绑定到特定实例;没有此指针,并且您没有“成员变量”.您可以将此指针作为参数传递给函数,然后将其转换为Dac *并从中访问成员变量.
所以你可以做
ping_thread = CreateThread (NULL , 0, ping_loop, (LPVOID)this, 0, &dping_thread);
并将您的ping_loop更改为:
static DWORD WINAPI ping_loop(void* param)
{
Dac* dac = (Dac*)param;
while ( dac->com.dac_ping() == 0)
Sleep(900);
return 1; //since this is an infinite loop, if the loop breaks, it has failed
}