I want to create a map whose key is a string and whose value is one of the member functions of a class. I originally had the functions outside the class and that could be called from a function in the class.
void nvmRead(unsigned char* msg, unsigned char* resp_frame);
void nvmWrite(unsigned char* msg, unsigned char* resp_frame);
void nvmProvision(unsigned char* msg, unsigned char* resp_frame);
void nvmStatus(unsigned char* msg, unsigned char* resp_frame);
std::map<std::string, void (*)(unsigned char* msg, unsigned char* resp_frame)> command_table = {
{ "NVM_READ", nvmRead },
{ "NVM_WRITE", nvmWrite },
{ "NVM_PROVISION", nvmProvision },
{ "NVM_STATUS", nvmStatus }
};
Then from a function in my class that receives a command request corresponding to one of the keys in the s variable it jumps to the appropriate function. I call the function using:
command_table.at(s)(parm1, parm2);
That got me to the appropriate function when they were defined external to the class.
I am trying to do the same thing but I want the above functions to be within the class, so that it can access private members of the class.
If I move the above lines of code into the class in the public section it seems to complain about not being able to convert the functions:
error: could not convert '{{"NVM_READ", ((myns::RaidDevCommInTask*)this)->myns::RaidDevCommInTask::nvmRead}, {"NVM_WRITE", ((myns::RaidDevCommInTask*)this)->myns::RaidDevCommInTask::nvmWrite}, {"NVM_PROVISION", ((myns::RaidDevCommInTask*)this)->myns::RaidDevCommInTask::nvmProvision}, {"NVM_STATUS", ((myna::RaidDevCommInTask*)this)->myns::RaidDevCommInTask::nvmStatus}}' from '<brace-enclosed initializer list>' to 'std::map<std::__cxx11::basic_string<char>, void (*)(unsigned char*, unsigned char*)>'
85 | };
I have tried various combinations that I won't go into here. Including making the assignments in a constructor doing something along the lines of
command_table["NVM_READ"] = nvmRead
which generates this error:
src/tests/raid_dev_comm_in_task.cpp:40:31: error: cannot convert 'myns::RaidDevCommInTask::nvmRead' from type 'void (myns::RaidDevCommInTask::)(unsigned char*, unsigned char*)' to type 'std::map<std::__cxx11::basic_string<char>, void (*)(unsigned char*, unsigned char*)>::mapped_type' {aka 'void (*)(unsigned char*, unsigned char*)'}
40 | command_table["NVM_READ"] = nvmRead;
| ^~~~~~~
Again trying lots of different combinations that I won't go into here.
I can't seem to find the correct combination.
How can I make a map where a string is used as the key and the value is a class' member function within the class?