GLUT callback registration (not quite OT)

  • Thread starter Martin Magnusson
  • Start date
M

Martin Magnusson

Hi all. I'm having trouble registering a GLUT callback function.
glutIdleFunc wants a void(*)() function as argument, and I believe
that's what I've given it, but gcc complains with the following message:

GraphicDisplay.cpp:15: error: no matches converting function `DrawC' to
type `void (*)()'
GraphicDisplay.hpp:11: error: candidates are: void GraphicDisplay::DrawC()

Can anyone see what I'm missing here?

//---begin code:

class GraphicDisplay
{
public:
void Init();
void DrawC();
};

void GraphicDisplay::Init()
{
// Standard GLUT init stuff...
// (snip)
glutIdleFunc( GraphicDisplay::DrawC );
}

void GraphicDisplay::DrawC()
{
// some code...
}

//---end code.
 
I

Ivan Vecerina

Martin Magnusson said:
GraphicDisplay.cpp:15: error: no matches converting function `DrawC' to
type `void (*)()'
GraphicDisplay.hpp:11: error: candidates are: void GraphicDisplay::DrawC()

Glut is a C library, and it expects a pointer to a C function.
So you cannot pass a non-static member function to it.

What will probably work is to replace:
void DrawC();
with:
static void DrawC();

But yes, this will force you to use global variables (DrawC will not
be able to access non-static member variables of class GraphicDisplay.


Note that, in theory, you also need to add an extern "C"
in front of the function's declaration (this is to formally
follow the standard, but this is rather rarly used in such
a context...)



hth,
 

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,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top