error: call of overloaded ‘bind(int (Class::*)(int, int), Class*, int, int)’ is ambiguous
error: call of overloaded ‘bind(int (Class::*)(int, int), Class*, int, int)’ is ambiguous
我正在尝试使用boost::bind,谁能告诉我以下代码段哪里错了?
#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)
我知道这是一个过载问题,但是头文件是自动包含的,接下来我该怎么做?
尝试去掉对 boost::bind 的调用中的 <int>
。你用的是什么版本的boost? boost 版本 1.58 有一个错误会导致这种歧义:https://svn.boost.org/trac/boost/ticket/11304
很简单,改变这一行:
boost::bind<int>(&Class::add, &cls, 1, 2);
到
boost::bind(&Class::add, &cls, 1, 2)();
bind
returns一个函数,你需要用operator()
.
调用它
顺便说一下,在 C++11 中不需要 boost::bind
。包括 <functional>
并使用 std::bind
.
我正在尝试使用boost::bind,谁能告诉我以下代码段哪里错了?
#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)
我知道这是一个过载问题,但是头文件是自动包含的,接下来我该怎么做?
尝试去掉对 boost::bind 的调用中的 <int>
。你用的是什么版本的boost? boost 版本 1.58 有一个错误会导致这种歧义:https://svn.boost.org/trac/boost/ticket/11304
很简单,改变这一行:
boost::bind<int>(&Class::add, &cls, 1, 2);
到
boost::bind(&Class::add, &cls, 1, 2)();
bind
returns一个函数,你需要用operator()
.
顺便说一下,在 C++11 中不需要 boost::bind
。包括 <functional>
并使用 std::bind
.