1

我正在尝试在对象中创建一个线程,但是我收到一条错误消息说'&' : illegal operation on bound member function expression.正在阅读我看到我必须使成员函数静态,但是当我这样做时我收到一条错误消息left of '.dac_ping' must have 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);
            }

    };
4

1 回答 1

4

static函数不绑定到特定实例;没有this指针,也没有“成员变量”。您可以将this指针作为参数传递给函数,然后将其转换为 aDac*并从中访问成员变量。

所以你可以做

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
}
于 2011-11-06T03:28:39.280 回答