C ++ - 迭代相同的签名成员函数

时间:2017-11-10 06:55:22

标签: class c++11 iteration function-pointers member

我有一个具有多个相同签名函数的类(创建将按优先级排序的数据结构)。我决定用循环替换写相同的签名函数调用26次,但由于某种原因它不起作用。有谁知道为什么下面的代码抛出以下错误消息?我一直在搜索,我发现的结果并没有让我更接近解决方案。基本上我的问题是为什么我从bind获取绑定助手,而不是正确的

#include <functional>
#include <vector>
#include <iostream>

using namespace std;

class interface{
/*some interface stuff*/
};

class dataRepresenter
{
    int i;
    int j;
};

class A
{
public:
    const dataRepresenter calculatorOfOneThing(const interface& i);
    const dataRepresenter calculatorOfAnotherThing(const interface& i);

    typedef std::function<const dataRepresenter(const interface&)> calculator;

    std::vector<calculator> myCalculators;

    A();

};

const dataRepresenter A::calculatorOfOneThing(const interface& i)
{
    cout <<"OneThing"<< endl;
}
const dataRepresenter A::calculatorOfAnotherThing(const interface& i)
{
    cout <<"Another"<< endl;
}

A::A()
{
        myCalculators.push_back(std::bind(&A::calculatorOfOneThing, this));
        myCalculators.push_back(std::bind(&A::calculatorOfAnotherThing,this));
}

int main( int argc, char **argv)
{
    interface i;
    A a;

    for(auto func : a.myCalculators)
        func(i);
    cout << "Success!" << endl;
    return 0;
}
main.cpp: In constructor ‘A::A()’:
main.cpp:42:68: error: no matching function for call to ‘std::vector<std::function<const dataRepresenter(const interface&)>>::push_back(std::_Bind_helper<false, const dataRepresenter (A::*)(const interface&), A*>::type)’
   myCalculators.push_back(std::bind(&A::calculatorOfOneThing, this));

0 个答案:

没有答案