Jonathan Gómez
Jonathan Gómez

Reputation: 443

Map error in the initialization with static functions

I tried to understand the error but no idea. The map looks like this:

std::map<std::string, IOperand *(Factory::*)(const std::string &)> _func

And trying to give the value like this:

_func["int8"] = &Factory::createInt8;

And how is my class:

class Factory
{
public:
    Factory();
    ~Factory();
    static IOperand *createOperand(eOperandType type, const std::string &value);

private:
    std::map<std::string, IOperand *(Factory::*)(const std::string &)> _func;
    static IOperand *createInt8(const std::string &value);
    static IOperand *createInt16(const std::string &value);
    static IOperand *createInt32(const std::string &value);
    static IOperand *createFloat(const std::string &value);
    static IOperand *createDouble(const std::string &value);
    static IOperand *createBigDecimal(const std::string &value);
};

And the error:

Assigning to 'std::map<std::__cxx11::basic_string<char>, IOperand *(Factory::*)(const std::__cxx11::basic_string<char> &), std::less<std::__cxx11::basic_string<char> >, std::allocator<std::pair<const std::__cxx11::basic_string<char>, IOperand *(Factory::*)(const std::__cxx11::basic_string<char> &)> > >::mapped_type' (aka 'IOperand *(Factory::*)(const std::__cxx11::basic_string<char> &)') from incompatible type 'IOperand *(*)(const std::string &)' (aka 'IOperand *(*)(const basic_string<char> &)')

The error is in assigning the value.

Upvotes: 0

Views: 157

Answers (3)

R Sahu
R Sahu

Reputation: 206717

The value type of the map is IOperand *(Factory::*)(const std::string &), which are non-static member functions of Factory. What you have in your class are static member functions. You can fix the problem by changing the value tpe of the map or by changing the functions.

Option 1

Change the value type.

std::map<std::string, IOperand *(*)(const std::string &)> _func;

Option 2

Change the functions.

IOperand *createInt8(const std::string &value);
IOperand *createInt16(const std::string &value);
IOperand *createInt32(const std::string &value);
IOperand *createFloat(const std::string &value);
IOperand *createDouble(const std::string &value);
IOperand *createBigDecimal(const std::string &value);

I'll let you figure out which one best serves your needs.

Upvotes: 4

arapelle
arapelle

Reputation: 55

IOperand *(Factory::*)(const std::string &);

describes a pointer to member function. What you try to assign is a pointer to function.

So, the type of your map should be:

std::map<std::string, IOperand *(*)(const std::string &)> _func;

Upvotes: 2

songyuanyao
songyuanyao

Reputation: 173024

Factory::createInt8 is static member function, the type of &Factory::createInt8 is not IOperand *(Factory::*)(const std::string &), but IOperand *(*)(const std::string &).

You can change the type of map to

std::map<std::string, IOperand *(*)(const std::string &)> _func;

Or make these functions non-static member functions.

Upvotes: 4

Related Questions