简体   繁体   中英

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. 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 ) . The proper syntax would be 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 . You need change the parameter to boost::function or make it a template.

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. I cannot tell if that will be an issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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