6

我有这个代码构建和运行完美:

boost::function<void(string)> bar = boost::bind(&Bar::BarHandler, this, _1);

//Somewhere else in Bar.cpp
void Bar::BarHandler( std::string message ){
    //Do stuff
}

当我在上面的代码中愉快地更改booststd,我开始收到此错误(我的编译器是 Visual Studio 2010 SP1):

c:\program files\microsoft visual studio 10.0\vc\include\xxpmfcaller(42): error C2664: 

'void (std::string)' : cannot convert parameter 1 from 'boost::arg<I>' to 'std::string'
1>          with
1>          [
1>              I=1
1>          ]
1>          No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>          c:\program files\microsoft visual studio 10.0\vc\include\xxpmfcaller(52) : see reference to function template instantiation '_Ret std::tr1::_Pmf_caller2<_Ret,_Arg0>::_Call_pmf<_Pmf,_Ty,_Farg1>(volatile const void *,_Pmf,_Farg0 &,_Farg1 &)' being compiled
1>          with
1>          [
1>              _Ret=_Rx,
1>              _Arg0=BarLib::Bar,
1>              _Pmf=void (__thiscall BarLib::Bar::* const )(std::string),
1>              _Ty=BarLib::Bar *,
1>              _Farg1=boost::arg<1>,
1>              _Farg0=BarLib::Bar *
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\xxcallpmf(9) : see reference to function template instantiation '_Ret std::tr1::_Pmf_caller2<_Ret,_Arg0>::_Apply_pmf<const _Ty,_Arg,boost::arg<I>>(_Pmf,_Farg0 &,_Farg1 &)' being compiled
1>          with
1>          [
1>              _Ret=_Rx,
1>              _Arg0=BarLib::Bar,
1>              _Ty=void (__thiscall BarLib::Bar::* const )(std::string),
1>              _Arg=BarLib::Bar *,
1>              I=1,
1>              _Pmf=void (__thiscall BarLib::Bar::* const )(std::string),
1>              _Farg0=BarLib::Bar *,
1>              _Farg1=boost::arg<1>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\xxbind1(292) : see reference to function template instantiation '_Ret std::tr1::_Callable_pmf<_Ty,_Memty,_Indirect>::_ApplyX<_Ret,_Arg&,boost::arg<I>&>(_Arg0,_Arg1) const' being compiled
1>          with
1>          [
1>              _Ret=_Rx,
1>              _Ty=void (__thiscall BarLib::Bar::* const )(std::string),
1>              _Memty=BarLib::Bar,
1>              _Indirect=false,
1>              _Arg=BarLib::Bar *,
1>              I=1,
1>              _Arg0=BarLib::Bar *&,
1>              _Arg1=boost::arg<1> &
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\xxbind0(31) : see reference to function template instantiation '_Ret std::tr1::_Bind2<_Callable,_Arg0,_Arg1>::_ApplyX<_Rx,std::basic_string<_Elem,_Traits,_Ax>&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&,std::tr1::_Nil&>(_Barg0,_Barg1,_Barg2,_Barg3,_Barg4,_Barg5,_Barg6,_Barg7,_Barg8,_Barg9)' being compiled
1>          with
1>          [
1>              _Ret=_Rx,
1>              _Callable=std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,
1>              _Arg0=BarLib::Bar *,
1>              _Arg1=boost::arg<1>,
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>,
1>              _Barg0=std::string &,
1>              _Barg1=std::tr1::_Nil &,
1>              _Barg2=std::tr1::_Nil &,
1>              _Barg3=std::tr1::_Nil &,
1>              _Barg4=std::tr1::_Nil &,
1>              _Barg5=std::tr1::_Nil &,
1>              _Barg6=std::tr1::_Nil &,
1>              _Barg7=std::tr1::_Nil &,
1>              _Barg8=std::tr1::_Nil &,
1>              _Barg9=std::tr1::_Nil &
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\xxcallobj(13) : see reference to function template instantiation 'void std::tr1::_Bind_base<_Ret,_BindN>::operator ()<_Arg0&>(_Carg0)' being compiled
1>          with
1>          [
1>              _Ret=void,
1>              _BindN=std::tr1::_Bind2<std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,BarLib::Bar *,boost::arg<1>>,
1>              _Arg0=std::string,
1>              _Carg0=std::string &
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\xxfunction(65) : see reference to function template instantiation '_Ret std::tr1::_Callable_obj<_Ty>::_ApplyX<_Rx,_Arg0&>(std::basic_string<_Elem,_Traits,_Ax>)' being compiled
1>          with
1>          [
1>              _Ret=void,
1>              _Ty=std::tr1::_Bind<void,void,std::tr1::_Bind2<std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,BarLib::Bar *,boost::arg<1>>>,
1>              _Rx=void,
1>              _Arg0=std::string,
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Ax=std::allocator<char>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\xxfunction(64) : while compiling class template member function 'void std::tr1::_Impl_no_alloc1<_Callable,_Rx,_Arg0>::_Do_call(_Arg0)'
1>          with
1>          [
1>              _Callable=_MyWrapper,
1>              _Rx=void,
1>              _Arg0=std::string
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\xxfunction(386) : see reference to class template instantiation 'std::tr1::_Impl_no_alloc1<_Callable,_Rx,_Arg0>' being compiled
1>          with
1>          [
1>              _Callable=_MyWrapper,
1>              _Rx=void,
1>              _Arg0=std::string
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\xxfunction(369) : see reference to function template instantiation 'void std::tr1::_Function_impl1<_Ret,_Arg0>::_Reset0o<_Myimpl,_Fty,std::allocator<_Ty>>(_Fty,_Alloc)' being compiled
1>          with
1>          [
1>              _Ret=void,
1>              _Arg0=std::string,
1>              _Fty=std::tr1::_Bind<void,void,std::tr1::_Bind2<std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,BarLib::Bar *,boost::arg<1>>>,
1>              _Ty=std::tr1::_Function_impl1<void,std::string>,
1>              _Alloc=std::allocator<std::tr1::_Function_impl1<void,std::string>>
1>          ]
1>          c:\program files\microsoft visual studio 10.0\vc\include\functional(113) : see reference to function template instantiation 'void std::tr1::_Function_impl1<_Ret,_Arg0>::_Reset<_Fx>(_Fty)' being compiled
1>          with
1>          [
1>              _Ret=void,
1>              _Arg0=std::string,
1>              _Fx=std::tr1::_Bind<void,void,std::tr1::_Bind2<std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,BarLib::Bar *,boost::arg<1>>>,
1>              _Fty=std::tr1::_Bind<void,void,std::tr1::_Bind2<std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,BarLib::Bar *,boost::arg<1>>>
1>          ]
1>          d:\svn.dra.workingcopy\acquisition\native\xraygenerator\gemammogenerator.cpp(101) : see reference to function template instantiation 'std::tr1::function<_Fty>::function<std::tr1::_Bind<_Result_type,_Ret,_BindN>>(_Fx)' being compiled
1>          with
1>          [
1>              _Fty=void (std::string),
1>              _Result_type=void,
1>              _Ret=void,
1>              _BindN=std::tr1::_Bind2<std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,BarLib::Bar *,boost::arg<1>>,
1>              _Fx=std::tr1::_Bind<void,void,std::tr1::_Bind2<std::tr1::_Callable_pmf<void (__thiscall BarLib::Bar::* const )(std::string),BarLib::Bar,false>,BarLib::Bar *,boost::arg<1>>>
1>          ]
1>
1>Build FAILED.

这是怎么回事?也许我应该删除对 Boost 标头的引用?

4

1 回答 1

17

您可能需要包含<functional>并显式使用占位符命名空间_1

using namespace std::placeholders;

编辑:正如评论中所指出的,您还需要删除将占位符引入全局命名空间的 boost 标头。

于 2012-04-17T17:08:53.340 回答