I am trying to assign a member function as the callback to an API method, but it return the below error message, I am not sure how to resolve this.
cannot convert ‘boost::function<int(rcv_t_stct*, msg_t_stct*)>*’ to ‘rcv_cb {aka int (*)(rcv_t_stct*, msg_t_stct*)}’
callback signature:
typedef int (*rcv_cb)(rcv_t *rcv, msg_t *msg);
callback setter:
create(rcv_cb cb);
my member function:
int receiver::on_received(rcv_t *rcv, msg_t *msg)
{
return 0;
}
attempted to assign to the setter:
boost::function<int (rcv_t *, msg_t *)> f2( boost::bind( &receiver::on_received, this, _1, _2) );
create(f2);
Solution:
I changed the receiver class to singleton, and used a static member function to wrap the non-static one.
int receiver::on_received(rcv_t *rcv, msg_t *msg)
{
return receiver::instance().on_received_private(rcv, msg);
}
Is this the best solution it can be to resolve this issue?
A boost::function<>
is not a free function, but a functor (type implementing operator()
). You cannot convert from boost::function<S>
to a pointer to a function with the signature S
. If it is within your control, you can change the callback setter to take a boost::function<>
rather than a free function. If that is outside of your control, then you cannot use a non-static member function for the callback.
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.