2011-10-28 51 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

相关问题