繁体   English   中英

boost :: bind帮助使用成员函数进行回调

[英]boost::bind help for callback with member function

您好,我是一名学生,正在研究使用成员函数回调的程序。 我碰巧使用了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*))’

有人可以帮我吗? 谢谢

首先,我很惊讶您没有收到void register_callback_datapoint(void(*)(datapoint_t *datapoint) cb_datapoint ) 正确的语法为void register_callback_datapoint(void(*cb_datapoint)(datapoint_t *datapoint)); 用于将函数指针声明为参数。

但是,问题是您试图传递boost::function ,它是一个函数对象 ,不能隐式转换为register_callback_datapoint的函数指针。 您需要更改参数以boost::function或使其成为模板。

void register_callback_datapoint(boost::function<void(datapoint_opaque_t*)> f);

要么

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

另外,我只是注意到了这一点,但是您的示例和编译错误不匹配。 一个说datapoint_opaque_t* ,另一个说datapoint_t* ,它们是不同的名称。 我不知道这是否会成为问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM