mardi 25 février 2020

Map object method using std::function

I want to map the Math object method in string in the following code

#include <functional>
#include <iostream>
#include <map>
#include <string>

struct Math
{
    double foo(double& x) { return 100.0; };
    double bar(double& x) { return 400.0; };
};

int main()
{
    Math m;
    std::map<std::string, std::function<double(double&)>> nn =
    {
        {"sin", [&m](double& x) {m.foo(x);}}
    };
}

The above code gives two errors, the first is

'initializing': cannot convert from 'initializer list' to 'std::map<std::string,std::function<double (double &)>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>'
        with
        [
            _Kty=std::string,
            _Ty=std::function<double (double &)>
        ]

and the second error:

no instance of constructor "std::map<_Kty, _Ty, _Pr, _Alloc>::map [with _Kty=std::string, _Ty=std::function<double (double &)>, _Pr=std::less<std::string>, _Alloc=std::allocator<std::pair<const std::string, std::function<double (double &)>>>]" matches the argument list

Please help, thank you

Aucun commentaire:

Enregistrer un commentaire