2016-07-25 10 views
0

boost :: bindを使用しようとしています。次のコードセグメントで間違っていますか?私のコンパイラでエラー:オーバーロードされた '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; 
} 

G ++ - 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(&Class::add, &cls, 1, 2)(); 

bind

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

を関数を返す、そしてあなたがそれを呼び出す必要が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);'行は実行されませんbind.hpp行(1158)の 'bind_t(F f、L const&l):f_(f)、l_(l){}'行までデバッグすると、bind_t 'boost :: bind (&Class :: add、&cls、1、2);'の行に戻ります。 、2); '行は今実行されません – neurobot

関連する問題