Invictus
Invictus

Reputation: 4338

Pointer to function Error While compilation

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'

Upvotes: 0

Views: 171

Answers (4)

LihO
LihO

Reputation: 42133

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 ;)

Upvotes: 2

Pubby
Pubby

Reputation: 53097

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

Upvotes: 3

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361812

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

Upvotes: 1

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272812

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.

Upvotes: 1

Related Questions