我正在尝试基于输入字符串的值将filter函数器映射到我的类的成员方法之一。
#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’吗?我建议用MyClass()显式地提供这个参数。
发布于 2012-07-19 05:27:55
这与boost::assign::map_list_of或std::map无关,只需执行以下命令即可重现相同的错误:
MyFilterFunctor mff;
auto bb = boost::bind(&MyClass::FilterFunction1, _1, _2, _3);
mff = bb;bb需要3个参数:MyClass、map<string,string>和string。mff需要两个参数,一个map<string,string>和一个string。两者显然是不相容的。
请尝试使用boost::bind(&MyClass::FilterFunction1, MyClass(), _1, _2))。
https://stackoverflow.com/questions/11546767
复制相似问题