0

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.

1
  • 2
    The compiler has written the whole answer for you: the needed type of the map values is IOperand *(*)(const std::string &), not IOperand *(Factory::*)(const std::string &). What does prevent your from changing the map declaration? Commented Jun 19, 2020 at 15:24

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

4

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.

Comments

2
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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.