Hello everyone.. I am a little bit stuck on this small problem.
I'm trying to understand callbacks but seem to be no understanding something.

It is probably something very simple. I've never used callbacks so am probably misunderstanding something here.
The below is pointless but I'm doing it to improve my understanding of callback functions.

On the line indicated below, I get the error:
invalid conversion from 'int ()*(int, int)' to 'int' [-fpermissive]**

#include <iostream>
#include <cstdlib>


using namespace std;

int add(int a, int b)
{
    return (a+b);
}

int multiply(int a, int(*x)(int t, int d))
{

    int r = x; //error occurs here: invalid conversion from 'int (*)(int, int)' to 'int' [-fpermissive]
    int e = a * r;
    return e;
}



int main()
{

    cout << "FUNCTION CALLBACKS" << endl;

   cout << multiply(10,add(5,2)) << endl;
    system("pause>null");
}

lines 12 and 15 are wrong. According to line 27 the parameter to multiply() is not a callback function, but just a simple integer.

int multiply(int a, int x)
{
    int r = x; 
    int e = a * r;
    return e;
}

If you really intend to pass the add() function as a parameter, then do something like this. Note that since add() takes two parameters, main() must supply those values on line 27, OR multiply() must supply them itself.

int multiply(int a, int(*x)(int, int),int b, int c )
{
    return a * x(b,c);

}   

If you want main() to supply the two parameters to main(), line 27 would look like this:

cout << multiply(10,add,2,5) << endl;

Otherwise if multiply() is to supply the two parameters

int multiply(int a, int(*x)(int, int) )
{
    return a * x(2,5);

}   

cout << multiply(10,add) << endl;
commented: Well constructed answer. +1

A quick Google turned up this ... (for starters)

http://www.cprogramming.com/tutorial/function-pointers.html

...

Callback Functions
Another use for function pointers is setting up "listener" or "callback" functions that are invoked when a particular event happens. The function is called, and this notifies your code that something of interest has taken place.

...

You might want to start with function pointers ... like you may wish to use when passing in a different compare function to a library sort ... to sort on different fields, etc... ?

Excellent.. thank you very much. This really clears things up for me. I realize now that I can only pass the function as the function is. The values must be passed separately or must already be at the destination. It's quiet clear now.

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

using namespace std;

int add(int a, int b)
{
    return (a+b);
}

int multiply(int a, int(*x)(int, int),int p, int z)
{

    int r = x(p,z);
    int e = a * r;
    return e;
}



int main()
{

   cout << "Calling the Multiply function and passing 'add' as an argument provides a value of =  " << multiply(10,add,5,2) << endl;
    system("pause>null");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.