我正在尝试将成员函数分配为 API 方法的回调,但它返回以下错误消息,我不确定如何解决此问题。
cannot convert ‘boost::function<int(rcv_t_stct*, msg_t_stct*)>*’ to ‘rcv_cb {aka int (*)(rcv_t_stct*, msg_t_stct*)}’
回调签名:
typedef int (*rcv_cb)(rcv_t *rcv, msg_t *msg);
回调设置器:
create(rcv_cb cb);
我的成员函数:
int receiver::on_received(rcv_t *rcv, msg_t *msg)
{
return 0;
}
试图分配给二传手:
boost::function<int (rcv_t *, msg_t *)> f2( boost::bind( &receiver::on_received, this, _1, _2) );
create(f2);
解决方案:
我将接收器类更改为单例,并使用静态成员函数来包装非静态的。
int receiver::on_received(rcv_t *rcv, msg_t *msg)
{
return receiver::instance().on_received_private(rcv, msg);
}
这是解决此问题的最佳解决方案吗?