Passing predicate function as a parameter to two functions?

Hello,

I am having some difficulty with passing a predicate function as a parameter to another function.

I receive an error... "Argument type of "bool" is incompatible with parameter of type "bool(*)(int, int)"

The main program calls either of the two bool functions (greater or less) and passes it as a parameter to the build function.

The build function then passes that as a parameter to another function.


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
bool greater(int i, int j)
{
	if (i > j)
		return true;
	else
		return false;
}

bool less(int i, int j)
{
	if (i < j)
		return true;
	else
		return false;
}

void heap(bool(*f)(int, int))
{
  //Nothing here yet
}

void build(bool(*f)(int, int))
{
     int a, b;

     bool result = (*f)(a, b);

     heap(result);               //ERROR - argument type of "bool" is 
                                 //incompatible with parameter of type "bool(*)(int, int)"
}

int main()
{
   build(greater);
   build(less);

   return 0;
}
The heap function expects a function pointer but you are passing a bool.
Right, I am just having a problem implementing that.

Would it be something like this?

1
2
3
4
void build(bool(*f)(int, int))
{
     heap((*f)(int, int));
}
f is the name of the function pointer, so to pass it to the function you simply write heap(f);

Thank you very much!! :)
Topic archived. No new replies allowed.