2016-07-25 6 views
0

boost :: bind를 사용하려고합니다. 다음 코드 세그먼트에서 잘못 되었습니까? 내 컴파일러 g ++에서오류 : 과부하 된 'bind (int (Class, *) (int, int), Class *, int, int)'호출이 불분명 함

#include <iostream> 
#include <boost/bind.hpp> 

using std::cout; 
using std::endl; 

class Class { 
public: 

    int add(int x, int y) { 
     cout << "x+y=" <<x+y<<endl; 
     return x+y; 
    } 
}; 

int main() 
{ 
    cout << "boost::thread: " << endl; 
    Class cls; 

    boost::bind<int>(&Class::add, &cls, 1, 2); 

    return 0; 
} 

- 4.7, 그것은 말한다 :

main.cpp: In function ‘int main()’: 
main.cpp:26:45: error: call of overloaded ‘bind(int (Class::*)(int, int), Class*, int, int)’ is ambiguous 
    boost::bind<int>(&Class::add, &cls, 1, 2); 
              ^
main.cpp:26:45: note: candidates are: 
In file included from /usr/include/boost/bind.hpp:22:0, 
       from main.cpp:3: 
/usr/include/boost/bind/bind.hpp:1610:5: note: boost::_bi::bind_t<R, F, typename boost::_bi::list_av_3<A1, A2, A3>::type> boost::bind(F, A1, A2, A3) [with R = int; F = int (Class::*)(int, int); A1 = Class*; A2 = int; A3 = int; typename boost::_bi::list_av_3<A1, A2, A3>::type = boost::_bi::list3<boost::_bi::value<Class*>, boost::_bi::value<int>, boost::_bi::value<int> >] 
    BOOST_BIND(F f, A1 a1, A2 a2, A3 a3) 
    ^
/usr/include/boost/bind/bind_mf_cc.hpp:109:5: note: boost::_bi::bind_t<R, boost::_mfi::mf2<R, T, A1, A2>, typename boost::_bi::list_av_3<A1, A2, A3>::type> boost::bind(R (T::*)(B1, B2), A1, A2, A3) [with R = int; T = Class; B1 = int; B2 = int; A1 = Class*; A2 = int; A3 = int; typename boost::_bi::list_av_3<A1, A2, A3>::type = boost::_bi::list3<boost::_bi::value<Class*>, boost::_bi::value<int>, boost::_bi::value<int> >] 
    BOOST_BIND(R (BOOST_BIND_MF_CC T::*f) (B1, B2), A1 a1, A2 a2, A3 a3) 
    ^
/usr/include/boost/bind/bind_mf_cc.hpp:131:5: note: boost::_bi::bind_t<Rt2, boost::_mfi::mf2<R, T, B1, B2>, typename boost::_bi::list_av_3<A1, A2, A3>::type> boost::bind(R (T::*)(B1, B2), A1, A2, A3) [with Rt2 = int; R = int; T = Class; B1 = int; B2 = int; A1 = Class*; A2 = int; A3 = int; typename boost::_bi::list_av_3<A1, A2, A3>::type = boost::_bi::list3<boost::_bi::value<Class*>, boost::_bi::value<int>, boost::_bi::value<int> >] 
    BOOST_BIND(R (BOOST_BIND_MF_CC T::*f) (B1, B2), A1 a1, A2 a2, A3 a3) 

나는 그가 과부하 문제를 알고 있지만, 헤더 파일이 자동으로 포함됩니다, 나는 다음에 무엇을해야하는지?

답변

0

간단한,이 라인 변경 :

boost::bind<int>(&Class::add, &cls, 1, 2); 

boost::bind(&Class::add, &cls, 1, 2)(); 

bind

에 함수를 반환합니다, 당신은 그것을 호출 할 필요가 operator().

그런데 C++ 11에서는 boost::bind이 필요하지 않습니다. <functional>을 포함하고 std::bind을 사용하십시오.

+0

감사합니다. – neurobot

0

boost :: bind를 호출 할 때 <int>을 제거하십시오. 어떤 버전의 부스트를 사용하고 있습니까? 부스트 버전 1.58이 모호함을 일으킬 수있는 버그가 있습니다 https://svn.boost.org/trac/boost/ticket/11304

+0

부스트 1.58을 사용하여 가이드를 따라 가면서 오류가 사라졌지만 여전히 boost :: bind (& Class :: add, & cls, 1, 2); 행이 실행되지 않습니다 cout을 stdin으로 출력하지 않는다.) bind.hpp 줄 (1158)에서'bind_t (F f, L const & l) : f_ (f), l_ (l) 생성자에서 호출 스택은'boost :: bind (& Class :: add, & cls, 1, 2); '줄 아래로'boost :: bind (& Class :: add, & cls, 1 , 2);'지금 실행하지 않습니다. – neurobot