bind () для использования функции-члена в качестве функции сравнения STL - PullRequest
0 голосов
/ 21 июля 2010

Может кто-нибудь сказать мне, почему следующее не скомпилируется?

#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 Ответ

6 голосов
/ 21 июля 2010

aComp - нестатическая функция-член B, поэтому вам также необходимо привязать указатель this:

tr1::bind(&B::aComp, this, tr1::placeholders::_1, tr1::placeholders::_2)
...