error compiling callbacks

J

Jake

I'm getting an error trying to do a callback. Is this a valid way to do
a callback in C++? The callback comes from a C library that I include
and link with.

MyClass.cpp: In member function 'void MyClass::functionA()':
MyClass.cpp:18: error: argument of type 'void (MyClass::)(const char*)'
does not match 'void (*)(const char*)'

---

#include <callbacks.h>

void MyClass::functionA() {
registerMyCallbackFunc(myCallbackFunc);
return;
}

void MyClass::myCallbackFunc(const char *id) {
printf("my id = %c\n", id);
return;
}
 
C

Chris M. Thomasson

I'm getting an error trying to do a callback. Is this a valid way to do a
callback in C++? The callback comes from a C library that I include and
link with.

MyClass.cpp: In member function 'void MyClass::functionA()':
MyClass.cpp:18: error: argument of type 'void (MyClass::)(const char*)'
does not match 'void (*)(const char*)'

---

#include <callbacks.h>

void MyClass::functionA() {
registerMyCallbackFunc(myCallbackFunc);
return;
}

void MyClass::myCallbackFunc(const char *id) {
printf("my id = %c\n", id);
return;
}


You need to use a free function decorated with extern "C". Try something
like:


_____________________________________________________________
extern "C" void myCallbackFunc(char const* id) {
printf("my id = %c\n", id);
}

void MyClass::functionA() {
registerMyCallbackFunc(myCallbackFunc);
}
_____________________________________________________________



BTW, you don't need to explicitly call return to exit from the end of a
procedures scope.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top