Parameter of function that has Pointer to a function

Could someone please help me? I am not sure how to solve this.

I have a header file named Person.h, where I have placed the function prototype

1
2
3
  		int WorkingWithPointers8(int, int, int(*)(int,int)); 
		int WorkingWithPointers9(int,int);  
		void WorkingWithPointers10(); 


In the Source file I have defined them as such

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int Person::WorkingWithPointers8(int i, int j, int(*pfunc)(int,int))
{
	//note the parameter defined for the pointer to a function.
	int a = pfunc(1,10);

	return a;


}

int Person::WorkingWithPointers9(int i, int j)
{
	return (i+j);
}


void Person::WorkingWithPointers10()
{
	int i = WorkingWithPointers8(10,11,WorkingWithPointers9); //This doesn't work for some reason

}



Thanks.
There is no WorkingWithPointers9 function in your code. THere is a Person::WorkingWithPointers9 one. Correct signature would be:
int WorkingWithPointers8(int, int, int(Person::*)(int,int));
And you cannot call nonstatic member function like pfunc(1,10);. You need an object to call it on.
Read this FAQ: https://isocpp.org/wiki/faq/pointers-to-members
Thanks.

I managed to get till here

In the Person.h

1
2
3
int WorkingWithPointers8(int, int, int(Person::*)(int,int));
int WorkingWithPointers9(int,int);
void WorkingWithPointers10();


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int Person::WorkingWithPointers8(int i, int j, int(Person::*pfunc)(int,int))
{	
	int a;

       a = pfunc(10,11); //HELP! - Unsure what exactly goes in here. Should I create the Person object?

	return a;


}

 int Person::WorkingWithPointers9(int i, int j)
{
	return (i+j);
}


void Person::WorkingWithPointers10()
{
	int i = WorkingWithPointers8(10, 11, &Person::WorkingWithPointers9); 

}


Thanks again.
Should I create the Person object?
Well, you cannot call nonstatic member function without object, so yes.
However, have you considered making WorkingWithPointers9 non-member function? Or static member function (which does not need object)?
Topic archived. No new replies allowed.