Function pointers generate an "invalid use of a non-static member function" error

I am trying to better understand the concept of pointer function. Therefore, I have a very simple and working example:

#include <iostream> using namespace std; int add(int first, int second) { return first + second; } int subtract(int first, int second) { return first - second; } int operation(int first, int second, int (*functocall)(int, int)) { return (*functocall)(first, second); } int main() { int a, b; int (*plus)(int, int); int (*minus)(int, int); plus = &add; minus = &subtract; a = operation(7, 5, add); b = operation(20, a, minus); cout << "a = " << a << " and b = " << b << endl; return 0; } 

So far so good. Now I need to group the functions in the class and select add or subtract based on the function pointer that I use. So I'm just doing a little modification like:

 #include <iostream> using namespace std; class A { public: int add(int first, int second) { return first + second; } int subtract(int first, int second) { return first - second; } int operation(int first, int second, int (*functocall)(int, int)) { return (*functocall)(first, second); } }; int main() { int a, b; A a_plus, a_minus; int (*plus)(int, int) = A::add; int (*minus)(int, int) = A::subtract; a = a_plus.operation(7, 5, plus); b = a_minus.operation(20, a, minus); cout << "a = " << a << " and b = " << b << endl; return 0; } 

and obvious error:

 ptrFunc.cpp: In function 'int main()': ptrFunc.cpp:87:29: error: invalid use of non-static member function 'int A::add(int, int)' ptrFunc.cpp:88:30: error: invalid use of non-static member function 'int A::subtract(int, int)' 

coz I did not specify which object to call (and so far I do not want to use static methods)

EDIT: a few comments and answers suggested that a non-static version (as I wrote) is not possible (thanks to everyone) So modifying the class as follows also doesn't work:

 #include <iostream> using namespace std; class A { int res; public: A(int choice) { int (*plus)(int, int) = A::add; int (*minus)(int, int) = A::subtract; if(choice == 1) res = operation(7, 5, plus); if(choice == 2) res = operation(20, 2, minus); cout << "result of operation = " << res; } int add(int first, int second) { return first + second; } int subtract(int first, int second) { return first - second; } int operation(int first, int second, int (*functocall)(int, int)) { return (*functocall)(first, second); } }; int main() { int a, b; A a_plus(1); A a_minus(2); return 0; } 

generated this error:

 ptrFunc.cpp: In constructor 'A::A(int)': ptrFunc.cpp:11:30: error: cannot convert 'A::add' from type 'int (A::)(int, int)' to type 'int (*)(int, int)' ptrFunc.cpp:12:31: error: cannot convert 'A::subtract' from type 'int (A::)(int, int)' to type 'int (*)(int, int)' 

Can I find out how to solve this problem?

thanks

+7
source share
4 answers

The syntax for declaring a function pointer to member methods is:

 int (A::*plus)(int, int) = &A::add; int (A::*minus)(int, int) = &A::subtract; 

To call member methods use. * or → * operator:

  (a_plus.*plus)(7, 5); 

Also see http://msdn.microsoft.com/en-us/library/b0x1aatf(v=vs.80).aspx

Hope this helps.

Full code:

  #include <iostream> using namespace std; class A { public: int add(int first, int second) { return first + second; } int subtract(int first, int second) { return first - second; } int operation(int first, int second, int (A::*functocall)(int, int)) { return (this->*functocall)(first, second); } }; int main() { int a, b; A a_plus, a_minus; int (A::*plus)(int, int) = &A::add; int (A::*minus)(int, int) = &A::subtract; a = a_plus.operation(7, 5, plus); b = a_minus.operation(20, a, minus); cout << "a = " << a << " and b = " << b << endl; return 0; } 
+5
source

You cannot pass non-static member functions as an argument, which is easy. And for your needs, I think it's better to redefine operators: http://www.learncpp.com/cpp-tutorial/92-overloading-the-arithmetic-operators/

But if you really need them as valid member functions, just make them static.

+2
source

See the code below. Function calls work without making them static.

 class A { public: int add(int first, int second) { return first + second; } int subtract(int first, int second) { return first - second; } int operation(int first, int second, int(A::*functocall)(int, int)) { return (this->*functocall)(first, second); } }; //typedef int(A::*PFN)(int, int) ; int main() { int a, b; A a_plus, a_minus; a = a_plus.operation(7, 5, &A::add); b = a_minus.operation(20, a, &A::subtract); cout << "a = " << a << " and b = " << b << endl; return 0; } 
+2
source

The editing you made in the code is still untrue, as it does not make static member functions. You need to do the functions of adding, subtracting, etc. Static by adding a static specifier:

 #include <iostream> using namespace std; class A { int res; public: A(int choice) { int (*plus)(int, int) = A::add; int (*minus)(int, int) = A::subtract; if(choice == 1) res = operation(7, 5, plus); if(choice == 2) res = operation(20, 2, minus); cout << "result of operation = " << res; } static int add(int first, int second) { return first + second; } static int subtract(int first, int second) { return first - second; } static int operation(int first, int second, int (*functocall)(int, int)) { return (*functocall)(first, second); } }; 
+1
source

All Articles