Archived

This topic is now archived and is closed to further replies.

Endy

Function pointer conversion problem...

Recommended Posts

I''m fairly new to C++ programming and have run into the following problem using Microsoft Visual C++. I am using the GLUT opengl library and trying to register a callback function. If I do it like this: void mainWindow1CallbackFunction(void); void main(int argc, char* argv[]) { blah blah blah glutDisplayFunc(mainWindow1CallbackFunction); blah blah blah } it works fine; however, if I try to put this in a class so: class foo { public: foo(); void mainWindowCallback(void); blah } void foo::foo() { blah blah blah glutDisplayFunc(this->mainWindow1CallbackFunction); blah blah blah } then windows complains about: : error C2664: ''glutDisplayFunc'' : cannot convert parameter 1 from ''void (void)'' to ''void (__cdecl *)(void)'' None of the functions with this name in scope match the target type C I''ve tried casting various ways but can''t get the compiler to be happy. I''d appreciate a suggestion on how to fix this. Regards, John

Share this post


Link to post
Share on other sites
quote:
Original post by Endy
I''ve tried casting various ways but can''t get the compiler to be happy. I''d appreciate a suggestion on how to fix this.

Member function pointers cannot be cast to pointers to non-member functions, simply because there is no place in a non-member function pointer for "this". You will have to rethink your design to register either a non-member or a static member s the callback.

Share this post


Link to post
Share on other sites