错误:重载的“bind(int(Class::*)(int,int),Class*,int,int”的调用不明确
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>(&Class::add, &cls, 1, 2);
至
boost::bind(&Class::add, &cls, 1, 2)();
bind
返回一个函数,您需要使用operator()
来调用它。
顺便说一句,您在C++11中不需要boost::bind
。包括<functional>
并使用std::bind
。
尝试在对boost::bind的调用中去掉<int>
。你使用的是什么版本的boost?boost版本1.58有一个错误,可能会导致这种歧义:https://svn.boost.org/trac/boost/ticket/11304
相关文章:
- 为什么在全局范围内使用"extern int a"似乎不行?
- int(c) 和 c-'0' 之间的区别。C++
- 从"int*"强制转换为"unsigned int"会丢失精度错误
- 尝试对 std::map<int, class> 进行排序时出错
- 错误:'class std::unique_ptr<std::set<long unsigned int> >'没有名为 'size' 的成员
- template<class T, int N> void h(T const(&)[N]); as friend function
- 为什么 fdump-class-hierarchy 为虚函数提供了两个指针 int vtable
- 如何就地构造 std::map<int, std::array<Class, 2>>?
- 在可变参数模板化函数中同时接受 int 和 class
- flann/util/serialization.h class std::unordered_map<unsigned int, std::vector<unsigned int>
- 为什么class {int i;};不是完全标准符合性的
- Java 等效于 C++ 的模板 <int val> class foo{ };
- c++ 中有标准'int class'吗?
- 链表函数 C++ "which is of non-class type 'int'" ?
- C++`int*class::*member=NULL`编译,原因是什么
- int 和 double 的均匀随机分布"base class"?
- int&& 和 template<class T > T&& 之间的区别
- Define namespace::int class
- 错误:重载的“bind(int(Class::*)(int,int),Class*,int,int”的调用不明确
- 不能将int {Class}::*转换为int*