Click to See Complete Forum and Search --> : callback functions


axr0284
June 17th, 2006, 06:20 PM
Hi,
I have been trying to use callback functions without any success. I tried to follow the example B on this webpage:
http://www.newty.de/fpt/callback.html#member

Here is the code:

class aodv_rit
{
public:

static void recvPacket_callback(unsigned char *packet, int size);

int recvPacket(unsigned char *packet, int size);

}

void aodv_rit::recvPacket_callback(unsigned char *packet, int size) {

// explicitly cast to a pointer to TClassA
aodv_rit* mySelf = (aodv_rit*) pt2Object;

// call member
mySelf->recvPacket(packet, size);

}

int aodv_rit::recvPacket(unsigned char * packet, int size)
{
...
..
...
}



This callback function is used here:

returnValue = emuInit(recvPacket_callback,maxPacketSize,ID,maxNumNodes);


I have also tried these without success giving the same error:

returnValue = emuInit(&recvPacket_callback,maxPacketSize,ID,maxNumNodes);

returnValue = emuInit(aodv_rit::recvPacket_callback,maxPacketSize,ID,maxNumNodes);

returnValue = emuInit(&(aodv_rit::recvPacket_callback),maxPacketSize,ID,maxNumNodes);



emuInit is declared as follows:

int filterPacket::init(int (*callbackFunction)(unsigned char *, int), int maxBytes, unsigned char id, int numNodes)



I am getting this error from ms visual studio 2006:
error C2664: 'int (int (__cdecl *)(unsigned char *,int),int,unsigned char,int)' : cannot convert parameter 1 from 'overloaded-function' to 'int (__cdecl *)(unsigned char *,int)'

I am really confused about what could be wrong. I really hope some of the more knowledgeable people can help out. Thanks
Amish

Marc G
June 18th, 2006, 02:43 AM
Because you have:
int filterPacket::init(int (*callbackFunction)(unsigned char *, int), ...
and you actual callback is:
void aodv_rit::recvPacket_callback(unsigned char *packet, int size)
The return type doesn't match.

axr0284
June 18th, 2006, 09:27 PM
Sweet thanks for the help.
Amish