Based on post How to call a function by its name (std::string) in C++?, tried to make a version using CLASS, but my approach does not work.
class A {
public:
int add(int i, int j) { return i+j; }
int sub(int i, int j) { return i-j; }
};
typedef int (*FnPtr)(int, int);
int main(int argc, char* argv[]) {
// initialization:
std::map<std::string, FnPtr> myMap;
A a;
myMap["add"] = a.add;
myMap["sub"] = a.sub;
Returns this erro:
main.cpp:31:22: error: cannot convert ‘A::add’ from type ‘int (A::)(int, int)’ to type ‘std::map<std::basic_string<char>, int (*)(int, int)>::mapped_type {aka int (*)(int, int)}’
main.cpp:32:22: error: cannot convert ‘A::sub’ from type ‘int (A::)(int, int)’ to type ‘std::map<std::basic_string<char>, int (*)(int, int)>::mapped_type {aka int (*)(int, int)}’
Does anyone know what is the error?
At least as you've shown things, your
class A
provides nothing but problems. If you turn it into a namespace, things will be a lot easier.This way,
add
andsub
aren't member functions, so you don't get the type mismatch. At least as shown, the instance ofA
provided no functionality beyond callingadd
andsub
, so a namespace accomplishes just as much good while eliminating the problems.You could also solve the problem, as mentioned by Kal in the comments, using std::function and std::bind(this is C++11 so):