C++ boost::使用成员函数绑定回调帮助

C++ boost::使用成员函数绑定回调帮助,c++,boost,callback,boost-bind,C++,Boost,Callback,Boost Bind,您好,我是一名学生,正在编写一个使用成员函数回调的程序。我发现bind的使用正是我所需要的。我只是很难让它工作 下面是相关的代码和编译错误 // this is the API function to register callback void register_callback_datapoint(void(*)(datapoint_t *datapoint) cb_datapoint ) // this function is my callback void datapoin

您好,我是一名学生,正在编写一个使用成员函数回调的程序。我发现bind的使用正是我所需要的。我只是很难让它工作

下面是相关的代码和编译错误

 // this is the API function to register callback
 void register_callback_datapoint(void(*)(datapoint_t *datapoint) cb_datapoint ) 

 // this function is my callback
 void datapoint_update(datapoint_t* datapoint);

 // this code is called in the aggregateThread class
 boost::function<void(datapoint_t*)> f;
 f = bind(&aggregateThread::datapoint_update, this, std::tr1::placeholders::_1);
 register_callback_datapoint(f);

 // here is the compile error
 cannot convert ‘boost::function<void(datapoint_opaque_t*)>’ to ‘void (*)(datapoint_t*)
 {aka void (*)(datapoint_opaque_t*)}’ for argument ‘1’ to ‘void 
 register_callback_datapoint(void (*)(datapoint_t*))’
//这是注册回调的API函数
无效寄存器\回调\数据点(无效(*)(数据点\ t*数据点)cb\数据点)
//这个函数是我的回调函数
无效数据点更新(数据点t*数据点);
//此代码在aggregateThread类中调用
boost::函数f;
f=bind(&aggregateThread::datapoint_update,this,std::tr1::占位符::_1);
寄存器回调数据点(f);
//下面是编译错误
无法将“boost::function”转换为“void(*)(数据点)
参数“1”到“void”的{aka void(*)(datapoint_不透明_t*)}
寄存器\u回调\u数据点(void(*)(数据点\u t*)'

有人能帮我吗?谢谢

首先,我很惊讶您在
无效寄存器\u回调\u数据点(void(*)(datapoint\u t*datapoint)cb\u数据点)中没有出现错误
。正确的语法应该是
void register\u callback\u datapoint(void(*cb\u datapoint)(datapoint\u t*datapoint))

但是,问题是您试图传递一个
boost::function
,它是一个函数对象,不能隐式转换为指向
寄存器\回调\数据点的函数指针。您需要将参数更改为
boost::function
或将其作为模板

void register_callback_datapoint(boost::function<void(datapoint_opaque_t*)> f);
void register\u callback\u datapoint(boost::function f);

模板
无效寄存器回调数据点(Func f);
另外,我刚刚注意到了这一点,但是您的示例和编译错误不匹配。一个说是
datapoint\u不透明的\u t*
,另一个说是
datapoint\u t*
,这是不同的名称。我不知道这是否会成为一个问题

template <typename Func>
void register_callback_datapoint(Func f);