【发布时间】:2012-07-18 17:11:41
【问题描述】:
我正在尝试根据输入字符串的值将过滤器仿函数映射到我的类的成员方法之一。
#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() 显式提供它?
【问题讨论】:
-
是的,filterKey 是一个 std::string。我将编辑一个变量声明以使其更清晰。
-
一个自包含的可编译的最小示例,人们可以复制粘贴并尝试自己玩,在这里可能会有所帮助。
-
@PlasmaHH 谢谢你的建议,我刚做了。
标签: c++ functor boost-bind boost-function