我想把一個成員函數作爲回調函數分配給一個API方法,但是它返回下面的錯誤信息,我不知道如何解決這個問題。用boost :: bind和boost :: function賦值回調成員函數
cannot convert ‘boost::function<int(rcv_t_stct*, msg_t_stct*)>*’ to ‘rcv_cb {aka int (*)(rcv_t_stct*, msg_t_stct*)}’
回調簽名:
typedef int (*rcv_cb)(rcv_t *rcv, msg_t *msg);
回調二傳手:
create(rcv_cb cb);
我的成員函數:
int receiver::on_received(rcv_t *rcv, msg_t *msg)
{
return 0;
}
試圖分配給二傳手:
boost::function<int (rcv_t *, msg_t *)> f2(boost::bind(&receiver::on_received, this, _1, _2));
create(f2);
解決方案:
我改變了接收器類獨居,和所使用的靜態成員函數來包裝非靜態的。
int receiver::on_received(rcv_t *rcv, msg_t *msg)
{
return receiver::instance().on_received_private(rcv, msg);
}
這是解決此問題的最佳解決方案嗎?
你可以改變回調簽名'boost :: function'? –
不,我不能。它是第三方庫 –