0

I am trying to learn the concept of pointer to function . I have written a code Which is throwing errors Which i could not decipher . Please have a look

     # include<iostream>
     # include<stdio.h>
     # include<conio.h>

     using namespace std;
     typedef int(*pt2Func)(int,int);

     class A
     {
           private : int x;
                     int y;
           public:
                  A(){}
                  A(int a, int b)
                  {
                   x=a;
                   y=b;
                  } 
                  int sum(int a, int b){return a+b;}
                  int sub( int a , int b){return a-b;}
                  int mult( int a, int b){return a*b;}
                  pt2Func GetPtr2(const char c)
                  {
                      if (c == '+')
                      return &sum;    // line 25
                      else if(c== '-')
                      return &sub;    // line 27
                      else if(c=='*')
                      return &mult;   //line 29
                  }
                  void pass_ptr(int (*pointer_fn)(int,int))
                  {
                   int result;
                   result=(*pointer_fn)(10,5);
                   cout << " result is : " << result;
                  }
                   ~A(){}
     };

     int main()
     {
        A a(0,5); 
        pt2Func=(a.GetPtr2)('+');          //line 43
        int result = (a.*pt2Func)(5,10);   //line 44
        cout << "result is " << result;
        getch();
        return 0;  
     }

On compiling this program, I get the following errors in line 25,27,29:

cannot convert `int (A::*)(int, int)' to `int (*)(int, int)' in return 

I also get error in line 43 and 44 Which are

expected primary-expression before='token'
2
  • 1
    Tip: when dealing with function pointers, create your typedef and then just use it. Commented Feb 19, 2012 at 14:01
  • 1
    declaring my typedef as typedef int(A::*pt2Func)(int,int); removes few of the error but i am still left with error like :-ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say &A::sum'` Commented Feb 19, 2012 at 14:04

4 Answers 4

3

Pointer to functions are not the same as pointer to (non-static) member functions.

There are a few ways to fix your program, I will outline them:

  • Use free functions or static instead of member functions
  • Change the type to a pointer to member ((A::*)(int, int))
  • Use std::function / std::bind
Sign up to request clarification or add additional context in comments.

Comments

2

You need to replace typedef int(*pt2Func)(int,int); with:

 class A;                                 // <-- forward declaration
 typedef int (A::*pt2Func)(int,int);

Then you need to replace return &sum; with return &A::sum; so that it matches type that you have defined.

And you also need to replace these lines:

pt2Func=(a.GetPtr2)('+');         // <-- pt2Func is type, name of variable is missing
int result = (a.*pt2Func)(5,10);  // <-- type name (pt2Func) is not allowed here

with these:

pt2Func ptr = a.GetPtr2('+');
int result = (a.*ptr)(5, 10);

Then it will work as it was intended to work ;)

Comments

1

Pointers-to-member-function are not the same as pointers-to-function. I suggest reading the C++ FAQ section dedicated to this topic: [33] Pointers to member functions.

Comments

1

The function sum() is a non-static member function, and it's type is not int (*)(int,int). It's type is int (A::*)(int,int), as it is shown in the compiler error message. The same is true of other two functions : sub, and mult.

There are two solutions. The simple solution is to make these functions static member function, then everything in your program would work without much change, except the following:

//pt2Func=(a.GetPtr2)('+'); //line 43 - error
pt2Func=a.GetPtr2('+');     //line 43 - corrected

//int result = (a.*pt2Func)(5,10); //line 44 - error
int result = pt2Func(5,10);        //line 44 - corrected

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.