function pointer

I need help on function pointer as i am new to this topic. I need to use function pointer at class level rather then at global level.

Following is the code:

TestFunctionPtr.h

#include<iostream>
using namespace std;

class TestFunctionPtr {
public:
int ADD(int a, int b);
int SUB(int a, int b);
int MUL(int a, int b);
int DIV(int a, int b);
static int (*funcPtr)(int , int);
};

TestFunctionPtr.C
#include "TestFunctionPtr.h"

int TestFunctionPtr::ADD(int a, int b){return a+b;}
int TestFunctionPtr::SUB(int a, int b){return a-b;}
int TestFunctionPtr::MUL(int a, int b){return a*b;}
int TestFunctionPtr::DIV(int a, int b){return a/b;}

TestFunctionCall.C
#include"TestFunctionPtr.h"



class TestFunctionCall {


public:
int Calculate(int (TestFunctionPtr::*funcPtr)(int,int), int a ,int b)
{
int result = *funcPtr(a,b);
return result;
}
};

int main()
{
TestFunctionCall *call = new TestFunctionCall();
TestFunctionPtr *ptr = new TestFunctionPtr();

call->Calculate(&ptr->ADD, 4, 5);
return 0;
}

Following is the error during compilation:

$ g++ -o TestfunctionP TestFunctionCall.C TestFunctionPtr.o
TestFunctionCall.C: In member function `int TestFunctionCall::Calculate(int (Tes
tFunctionPtr::*)(int, int), int, int)':
TestFunctionCall.C:11: error: must use .* or ->* to call pointer-to-member funct
ion in `funcPtr (...)'
TestFunctionCall.C: In function `int main()':
TestFunctionCall.C:21: error: ISO C++ forbids taking the address of a bound memb
er function to form a pointer to member function. Say `&TestFunctionPtr::ADD'
[code]Your code goes here.[/code]
TestFunctionCall.C:11: error: must use .* or ->* to call pointer-to-member function in `funcPtr (...)'
You need an object to call a member function (if is not static).

TestFunctionCall.C:21: error: ISO C++ forbids taking the address of a bound member function to form a pointer to member function. Say `&TestFunctionPtr::ADD'
Do what it says

1
2
TestFunctionCall *call = new TestFunctionCall();
TestFunctionPtr *ptr = new TestFunctionPtr();
This is not Java. You can have objects.
I have made all the function of Class TestFunctionPtr as static. Still i am getting the following error:

$ g++ -o qwerty TestFunctionCall.C TestFunctionPtr.o
TestFunctionCall.C: In member function `int TestFunctionCall::Calculate(int (Tes
tFunctionPtr::*)(int, int), int, int)':
TestFunctionCall.C:11: error: must use .* or ->* to call pointer-to-member funct
ion in `funcPtr (...)'
TestFunctionCall.C: In function `int main()':
TestFunctionCall.C:21: error: no matching function for call to `TestFunctionCall
::Calculate(int (*)(int, int), int, int)'
TestFunctionCall.C:10: note: candidates are: int TestFunctionCall::Calculate(int
(TestFunctionPtr::*)(int, int), int, int)
1
2
//int Calculate(int (TestFunctionPtr::*funcPtr)(int,int), int a ,int b)
int Calculate(int (*funcPtr)(int,int), int a ,int b)

I'm not sure if you need to call with &ptr->ADD or just ptr->ADD (or if there is a difference)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
using namespace std;

class TestFunctionPtr {
public:
int ADD(int a, int b);
int SUB(int a, int b);
int MUL(int a, int b);
int DIV(int a, int b);
static int (*funcPtr)(int , int);
};

TestFunctionPtr.C
#include "TestFunctionPtr.h"

int TestFunctionPtr::ADD(int a, int b){return a+b;}
int TestFunctionPtr::SUB(int a, int b){return a-b;}
int TestFunctionPtr::MUL(int a, int b){return a*b;}
int TestFunctionPtr::DIV(int a, int b){return a/b;}

TestFunctionCall.C
#include"TestFunctionPtr.h"



class TestFunctionCall {


public:
int Calculate(int (TestFunctionPtr::*funcPtr)(int,int), int a ,int b)
{
int result = *funcPtr(a,b);
return result;
}
};

int main()
{
TestFunctionCall *call = new TestFunctionCall();
TestFunctionPtr *ptr = new TestFunctionPtr();

call->Calculate(&ptr->ADD, 4, 5);
return 0;
}


Just so anyone who is helping can read your code. This is beyond me at this point ( working on Classes), no pointers for me yet.
Topic archived. No new replies allowed.