c++ - Error converting void(__cdecl MyClass::*)() to void * -
i trying link external library in qt application. external library has header file following relevant code i'm trying call:
extern vgrabdevice_api bool v_assignframesizecallback(igrabchannel* pchannel, void* pfunc); in demo c++ program provided, has no problems compiling, following relevant code is:
// in main.cpp void _stdcall myframesizecallback(t x) { do_stuff; } int main(int argc, char* argv[]) { igrabchannel* pchannel0 = something; v_assignframesizecallback(pchannel0, myframesizecallback); } i trying incorporate code qt application, getting problems. in mainwindow.cpp file:
void _stdcall mainwindow::myframesizecallback(t x) { do_stuff; } void mainwindow::somefunction() { igrabchannel* pchannel0 = something; v_assignframesizecallback(pchannel0, &mainwindow::myframesizecallback); } the error i'm getting is:
error: c2664: 'bool v_assignframesizecallback(igrabchannel *,void *)' : cannot convert argument 2 'void (__cdecl mainwindow::* )(t)' 'void *' there no context in conversion possible what need do? thanks.
you have 2 problems. first, void* data pointer, not function pointer. according c++ standard, casting between 2 not expected work. platforms provide stronger guarantee... example windows getprocaddress , *nix dlsym mix two.
next, &mainwindow::myframesizecallback not function pointer, pointer-to-member-function. calling requires mainwindow object, external library doesn't know about.
you need provide ordinary function, not member function, library. if have way ahold of mainwindow* object pointer, can call member function real work. library provides "context" parameter passed callback; that's great place put object pointer. otherwise, you'll need store mainwindow* in global variable. easy if have one, while if have more 1 might go std::map<igrabchannel*, mainwindow*>.
code:
mainwindow* mainwindow::the_window; void mainwindow::myframesizecallback(t x) { do_stuff; } void _stdcall myframesizecallbackshim(t x) { mainwindow::the_window->myframesizecallback(x); } void mainwindow::somefunction() { igrabchannel* pchannel0 = something; the_window = this; v_assignframesizecallback(pchannel0, &myframesizecallbackshim); } if parameter x isn't igrabchannel, change map datatype , insertion logic accordingly. if parameter x isn't sort of unique predictable identifier, may limited doing callbacks 1 mainwindow instance.
Comments
Post a Comment