c++ - 错误 : ambiguous overload for ‘operator[]’ when using boost-bind to boost-function

标签 c++ functor boost-bind boost-function

我正在尝试根据输入字符串的值将过滤器仿函数映射到我的类的一个成员方法。

#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/

相关文章:

c++ - Boost绑定(bind)并分配以将 vector 转换为字符串

c++ - 将值 vector 复制到一行中的对 vector

c++ - 为太空模拟器/游戏旋转 spaceship 模型

c++ - 多态数组找出其中有哪些 child

c++ - 如何在 C++ 中为多个类实现公共(public)仿函数

haskell - Haskell 中附加词的用例

c++ - 如何使用/操作嵌套 boost::bind 的返回值

c++ - 使用 VAO/VBO 的 OpenGL 模型/纹理渲染

python - 当 QApplication 退出时,QT 引发段错误(核心已转储)

C++ store functor 在类中