C and C++ Signatures

(A question was asked regarding using a C++ method instead of a C function for use in a C callback.)

I'm going to go off memory here, I'm pretty sure this is correct but forgive me if it isn't 100%.

error C2664: 'glutIdleFunc' : cannot convert parameter 1 from 'void (void)' to 'void (__cdecl *)(void)'

None of the functions with this name in scope match the target type
glutIdleFunc(display);

void Assignment_1bv2::display()

Declare your assignment display method in your class as static, or create C-style callbacks (that can then call your class), like so:

void cstyledisplay()
{
  myclass.display();
}
 
glutIdleFunc(cstyledisplay);

Where myclass is your global instance.

Or a static that calls the global instance…

class assignment2
{
  public:
   static void cstyledisplay()
   {
     myclass.display();
   }
   void display()
   {
    ...
   }
};

Where myclass is your global instance.

This is typically how you would implement C and C++ interaction in real world applications, so it's a pretty common practice.

I'm not sure how to troubleshoot this, since putting everything in one cpp file doesn't give me this error.

I'm willing to bet that this is unrelated; you've gone from C functions to C++ methods. They have different signatures. Even if you were to use a type cast in the function call(don't), it wouldn't work (and would likely crash) because non-static C++ methods still have a “this” pointer on the stack, whereas C functions don't.

Terence J. Grant 08/31/2006 09:58