You cannot save a function on the map โ just a pointer to a function. With some other minor details cleared, you will get something like this:
#include <map> #include <iostream> #include <string> std::map<std::string, void(*)()> commands; void method() { std::cout << "IT WORKED!"; } void Run() { commands["a"](); } int main(){ commands["a"] = method; Run(); }
At least with g ++ 4.7.1, this prints IT WORKED! as you apparently wanted / expected.
source share