2012-02-09 121 views
5

我試圖在我的記錄器類中創建std :: functions的向量。當我嘗試結合的方法,以我的std ::函數那樣:在C++中使用std綁定函數的參數(字符串)

NcursesWindow log_win("Logs",LINES-1,COLS/3,0,COLS*2/3); 
std::function<void(std::string)> f = std::bind(&NcursesWindow::add_string,&log_win); 

的add_string功能被定義,如:

void add_string(string text); 

然而,海灣合作委員會(與gfilt插件,試圖瞭解模板錯誤)返回:

BD Software STL Message Decryptor v3.10 for gcc 2/3/4 
In file included from ./inc/ncursesui.h:6:0, 
from src/ncursesui.cpp:1: 
functional: In static member function ‘static void _Function_handler< 
    void({basic_string<char>} ...), _Bind< 
     _Mem_fn<void (NcursesWindow::*)(basic_string<char>)>(
      NcursesWindow)> 
>::_M_invoke(const _Any_data &, {basic_string<char>} ...)’: 
[STL Decryptor: Suppressed 1 more STL standard header message] 
src/ncursesui.cpp:32:86: instantiated from here 
functional:1778:2: erreur: no match for call to ‘(
    _Bind< 
     _Mem_fn<void (NcursesWindow::*)(basic_string<char>)>(
      NcursesWindow)>) (basic_string<char>)’ 

STL Decryptor reminders: 
Use the /hdr:L option to see all suppressed standard lib headers 
Use the /cand:L option to see all suppressed template candidates 
+0

'add_string()'NcursesWindows'的成員函數嗎? – liwp 2012-02-09 14:28:57

+1

在綁定調用中缺少字符串參數的佔位符嗎?在boost中,你需要'bind(&NcursesWindow :: add_string,&log_win,_1)' – nabulke 2012-02-09 14:33:05

+0

是的,這個函數原型取自NcursesWindows .h – Tuxer 2012-02-09 14:36:53

回答

9

沒有爲string參數在綁定調用缺少一個佔位符?

嘗試這種情況:

bind(&NcursesWindow::add_string,&log_win,std::placeholders::_1); 

成員函數有兩個參數:隱藏this指針和std::string。您將第一個綁定到您的類的實例,另一個將保留,因此佔位符。