簡體   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