我正在尝试根据输入字符串的值将过滤器仿函数映射到我的类的一个成员方法。
#include <iostream>
#include <map>
#include <boost/function.hpp>
#include <boost/cstdint.hpp>
#include <boost/assign.hpp>
#include <boost/bind.hpp>
typedef boost::function < bool(std::map<std::string, std::string>, std::string) > MyFilterFunctor;
class MyClass
{
public:
bool FilterFunction1(std::map<std::string, std::string> myMap, std::string filterValue)
{
//do something
return true;
}
};
int main() {
MyFilterFunctor myFilter = boost::bind(&MyClass::FilterFunction1, _1, _2, _3);
return 0;
}
还有我的错误:
/usr/include/boost/bind/bind.hpp:375:
error: ambiguous overload for ‘operator[]’ in ‘a[boost::_bi::storage3<A1, A2, boost::arg<I> >::a3_ [with A1 = boost::arg<1>, A2 = boost::arg<2>, int I = 3]]’
编辑: 根据对我的问题的建议答案的建议,我稍微简化了示例。 有人建议我需要将 MyClass() 作为参数传递给 boost::bind,这确实解决了发布的代码段中的编译错误。但是,鉴于我的代码结构,我不可能这样做。我想知道为什么我所做的与 boost::bind 文档中的这个示例不同:
struct X
{
int f(int);
}
int main()
{
boost::bind(&X::f, 1); // error, X::f takes two arguments
boost::bind(&X::f, _1, 1); // OK
}
难道 _1 参数不应该处理隐含的“this”吗?建议我用 MyClass() 显式提供它?
最佳答案
这与boost::assign::map_list_of
无关或 std::map
, 可以简单地重现同样的错误:
MyFilterFunctor mff;
auto bb = boost::bind(&MyClass::FilterFunction1, _1, _2, _3);
mff = bb;
bb
需要 3 个参数:a MyClass
, 一个 map<string,string>
和一个 string
. mff
需要 2 个参数,一个 map<string,string>
和一个 string
.这两者显然是不相容的。
尝试 boost::bind(&MyClass::FilterFunction1, MyClass(), _1, _2))
相反。
关于c++ - 错误 : ambiguous overload for ‘operator[]’ when using boost-bind to boost-function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11546767/