[英]boost::bind help for callback with member function
Hello I am a student working on a program I have that uses a callback of a member function. 您好,我是一名学生,正在研究使用成员函数回调的程序。 I came across the use of bind which is exactly what I need.
我碰巧使用了bind,这正是我所需要的。 I just am having difficulties getting it working.
我只是很难让它工作。
Below is the relevant code and compile errors 下面是相关代码和编译错误
// 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*))’
Can someone help me with this please? 有人可以帮我吗? Thank you
谢谢
First, I am surprised you did not get an error with void register_callback_datapoint(void(*)(datapoint_t *datapoint) cb_datapoint )
. 首先,我很惊讶您没有收到
void register_callback_datapoint(void(*)(datapoint_t *datapoint) cb_datapoint )
。 The proper syntax would be void register_callback_datapoint(void(*cb_datapoint)(datapoint_t *datapoint));
正确的语法为
void register_callback_datapoint(void(*cb_datapoint)(datapoint_t *datapoint));
for declaring a function pointer as the parameter. 用于将函数指针声明为参数。
However, the problem is you are trying to pass a boost::function
, which is a function object and not implicitly convertible to a function pointer to register_callback_datapoint
. 但是,问题是您试图传递
boost::function
,它是一个函数对象 ,不能隐式转换为register_callback_datapoint
的函数指针。 You need change the parameter to boost::function
or make it a template. 您需要更改参数以
boost::function
或使其成为模板。
void register_callback_datapoint(boost::function<void(datapoint_opaque_t*)> f);
or 要么
template <typename Func>
void register_callback_datapoint(Func f);
Also, I just noticed this, but your example and compile error do not match. 另外,我只是注意到了这一点,但是您的示例和编译错误不匹配。 One says
datapoint_opaque_t*
and the other says datapoint_t*
which are different names. 一个说
datapoint_opaque_t*
,另一个说datapoint_t*
,它们是不同的名称。 I cannot tell if that will be an issue. 我不知道这是否会成为问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.