2011-10-28 52 views
3

我的目標是在調用for_each時使用成員函數。所以,我沒有這樣的:如何綁定一個成員函數以用於std :: for_each?

for_each(an_island->cells.cbegin(),an_island->cells.cend(),std::bind(&nurikabe::fill_adjacent,this)); 

,但是這是我在GCC獲得:

In file included from /usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/algorithm:63:0, 
       from prog.cpp:10: 
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h: In function '_Funct std::for_each(_IIter, _IIter, _Funct) [with _IIter = std::_Rb_tree_const_iterator<std::pair<int, int> >, _Funct = std::_Bind<std::_Mem_fn<int (nurikabe::*)(std::pair<int, int>)>(nurikabe*)>]': 
prog.cpp:85:103: instantiated from here 
/usr/lib/gcc/i686-pc-linux-gnu/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:4185:2: error: no match for call to '(std::_Bind<std::_Mem_fn<int (nurikabe::*)(std::pair<int, int>)>(nurikabe*)>) (const std::pair<int, int>&)' 

,並在VS2010,這樣的:

xxresult(28): error C2825: '_Fty': must be a class or namespace when followed by '::' 

全烴源代碼是here

任何幫助?

回答

4

nurikabe::fill_adjacent實際上有兩個參數 - 一個nurikabe*和一個cell - 但你只傳遞第一個參數。使用佔位符cell,像這樣:

for_each(
    an_island->cells.cbegin(), 
    an_island->cells.cend(), 
    std::bind(&nurikabe::fill_adjacent, this, _1) 
); 

(注意:_1駐留在命名空間std::placeholders

+0

我用'的std ::佔位符:: _ 1'(因爲,'_1'沒有工作) – vivek

+1

@vivek:對,這就是爲什麼我提到了它所在的命名空間的原因 - 所以你會知道如何完全限定它或使用哪個命名空間來使用using指令。 : - ] – ildjarn

相關問題