2010-07-21 93 views
0

有人能告诉我为什么下面不会编译?使用成员函数作为STL比较函数的bind()

#include "a.h" 
#include <list> 
#include <algorithm> 
#include <tr1/functional> 

using namespace std; 

class B { 
    public: 
    B() { 
     list< A* > aList; 
     A* a = new A(); 
     lower_bound(aList.begin(), aList.end(), a, tr1::bind(&B::aComp, tr1::placeholders::_1, tr1::placeholders::_2)); 
    } 
    private: 
    bool aComp(A* a1, A* a2); 
}; 

编译输出:

In file included from c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/tr1/functional:56, 
from ..\bindTest\/b.h:4, 
from ..\bindTest\b.cpp:1: 
c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/tr1_impl/functional: In member function 'typename std::tr1::result_of<_Functor(typename std::tr1::result_of<std::tr1::_Mu<_Bound_args, std::tr1::is_bind_expression::value, (std::tr1::is_placeholder::value > 0)>(_Bound_args, std::tr1::tuple<_UElements ...>)>::type ...)>::type std::tr1::_Bind<_Functor(_Bound_args ...)>::__call(const std::tr1::tuple<_UElements ...>&, std::tr1::_Index_tuple<_Indexes ...>) [with _Args = A*&, A* const&, int ..._Indexes = 0, 1, _Functor = std::tr1::_Mem_fn<bool (B::*)(A*, A*)>, _Bound_args = std::tr1::_Placeholder<1>, std::tr1::_Placeholder<2>]': 
c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/tr1_impl/functional:1191: instantiated from 'typename std::tr1::result_of<_Functor(typename std::tr1::result_of<std::tr1::_Mu<_Bound_args, std::tr1::is_bind_expression::value, (std::tr1::is_placeholder::value > 0)>(_Bound_args, std::tr1::tuple<_UElements ...>)>::type ...)>::type std::tr1::_Bind<_Functor(_Bound_args ...)>::operator()(_Args& ...) [with _Args = A*, A* const, _Functor = std::tr1::_Mem_fn<bool (B::*)(A*, A*)>, _Bound_args = std::tr1::_Placeholder<1>, std::tr1::_Placeholder<2>]' 
c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/bits/stl_algo.h:2495: instantiated from '_FIter std::lower_bound(_FIter, _FIter, const _Tp&, _Compare) [with _FIter = std::_List_iterator<A*>, _Tp = A*, _Compare = std::tr1::_Bind<std::tr1::_Mem_fn<bool (B::*)(A*, A*)>(std::tr1::_Placeholder<1>, std::tr1::_Placeholder<2>)>]' 
..\bindTest\/b.h:13: instantiated from here 
c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/tr1_impl/functional:1137: error: no match for call to '(std::tr1::_Mem_fn<bool (B::*)(A*, A*)>) (A*&, A* const&)' 
c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/tr1_impl/functional:546: note: candidates are: _Res std::tr1::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class&, _ArgTypes ...) const [with _Res = bool, _Class = B, _ArgTypes = A*, A*] 
c:\qt\2010.04\mingw\bin\../lib/gcc/mingw32/4.4.0/include/c++/tr1_impl/functional:551: note: _Res std::tr1::_Mem_fn<_Res (_Class::*)(_ArgTypes ...)>::operator()(_Class*, _ArgTypes ...) const [with _Res = bool, _Class = B, _ArgTypes = A*, A*] 
+1

最长的错误行有史以来** **。 oO – ereOn 2010-07-21 15:11:37

+3

@ereOn:不幸的是错误 - 这只是一个页面而已。我生成了一个长度超过四页的页面(并且包含一个单一的模板实例,当您扩展出所有模板参数时,这个实例的类型在页面上有一个名称)。 – 2010-07-21 15:31:55

回答

6

aCompB的非静态成员函数,所以你需要绑定到this指针,以及:

tr1::bind(&B::aComp, this, tr1::placeholders::_1, tr1::placeholders::_2) 
相关问题