类中的无效指针 - 铸造

时间:2015-03-21 07:26:58

标签: c++

我有一个c ++类,如下所示。我试图在下面设置一个线程,其中我的回调(void指针函数)在类中。这会在pthread_create函数上引发错误,表示

无法从void *(Ball ::)(void *)类型转换为void *()(void

class Ball
{
    private: struct BallMsg ballMsg;
    private: pthread_t ballThread;


    public: Ball(int id)
    {
        ballMsg.id = id;
        ballMsg.r = 7;
        ballMsg.x = 60 + (455/2) - 30;
        ballMsg.y = 60 + 10 + 5 + ballMsg.r;
        ballMsg.c = 0xFFFF0000;
        ballMsg.vel = 5.0;
        ballMsg.ang = 45.0;

        int ret;
        ret = pthread_create(&ballThread, NULL, this->ball_thread, NULL);
        if (ret !=0)
        {
            xil_printf("Error launching thread 1\r\n");
        }
    }

    public: void *ball_thread(void *arg)
    {
        int i = 0;
        while(1)
        {
            //unsigned int tval = sys_time(NULL);
            //xil_printf("%d : Time is: %d\r\n", i++, tval);
        }
    }

    public: ~Ball()
    {

    }
};

1 个答案:

答案 0 :(得分:3)

pthread_create需要指向非成员函数的指针,但void *ball_threadBall的成员。这意味着它需要一个Ball对象被调用,因此与非成员有一个根本不同的结构。

您可以使用std::thread代替,这样可以轻松地将对象绑定到成员函数。

或者,您可以ball_thread为非会员提供服务,并在必要时传递Ball*第一个参数。

相关问题