Dimitri Danilov Dimitri Danilov - 2 months ago 19
C++ Question

Why my map of pointer to function isn't compiling?

I have this class :

class TaskManager

using taskFunction = void (TaskManager::*)(BabelPacket const &);
void executeTask(Task const &task);
void signInTask(BabelPacket const &);
void signUpTask(BabelPacket const &);
void signOutTask(BabelPacket const &);
std::map<unsigned int, taskFunction> actions =
{BabelPacket::Code::SIGN_IN, &TaskManager::signInTask},
{BabelPacket::Code::SIGN_UP, &TaskManager::signUpTask},
{BabelPacket::Code::SIGN_OUT, &TaskManager::signOutTask},

And this is my enum

struct BabelPacket
// Code enum
enum struct Code : unsigned int
NONE = 00,
// Requests
SIGN_IN = 100,
SIGN_UP = 101,
SIGN_OUT = 102,

But when I compile this I got :

includes/TaskManager.hpp:38: error: could not convert ‘{{SIGN_IN, &TaskManager::signInTask}, {SIGN_UP, &TaskManager::signUpTask}, {SIGN_OUT, &TaskManager::signOutTask}}’ from ‘<brace-enclosed initializer list>’ to ‘std::map<unsigned int, void (TaskManager::*)(const BabelPacket&)>’

Got an idea why ?


Since your enum is a class enum, it is not automatically convertible to int. Either make a regular enum out of it, our use static_cast, or use BabelPacket::Code as your map index type.