I have been trying to map a string to a function, but after testing and reading uncountable posts I still can't figure it out. I'm very new to C++, so excuse me if I say something weird here.
The error I'm getting is the following:
error: called object type 'void (test::*)()' is not a function or function pointer find->second(type);
The header has the mappings and the initializers, all of them are private.
void printInfoHello();
void printInfopHi();
void prinInfoBye();
std::map<std::string, void(test::*)()> infoMap = {
{"hello", &test::printInfoHello},
{"hi", &test::printInfopHi},
{"bye", &test::prinInfoBye}
};
Then in the cpp I tried to get the map to call the functions based on the string, which seems to work just fine when I pass something that doesn't exist in the mapping, but if I type "hi" it returns the error I mentioned above.
void test::processInput(std::string &input)
{
std::vector<std::string> val = explodeVals::split(input, ' ');
if (val.empty()) {
std::cout << "Empty input!" << std::endl;
return;
}
std::string type = val[0];
std::cout << "Is it taking the right type: " type << std::endl;
auto find = infoMap.find(type);
if (find == infoMap.end()) {
std::cout << "Error, input not found" << std::endl;
return;
}
find->second(type);
}
void printInfoHello(){
std::cout << "Saying hello!" << std::endl;
}
void printInfoHi(){
std::cout << "Saying hi!" << std::endl;
}
void printInfoBye(){
std::cout << "Saying good bye!" << std::endl;
}
Also, on a side note, I will need to extend this and some functions are going to receive inputs, like a vector (std::vectorstd::string &Input) or strings.
I'm also including the following libraries in both the .h and .cpp files.
#include <iostream>
#include <vector>
#include <map>
#include <functional>