Search code examples
c++functionglut

cannot convert 'classname::glutKeyboard' from type 'void (classname::)(unsigned char, int, int)' to type 'void (*)(unsigned char, int, int)'


I have create a kinect application in c++, but i have same error in my glut functions, void glutKeyboard, glutDisplay, glutIdle. in following example i'm the all function is declared in the main file, so no need class, but in my application need, however the class generates an error by declaring the scope of functions.

this and the declaration of the function header:

class VideoOpenGL : public QGLWidget
{
    Q_OBJECT
public:
    explicit VideoOpenGL(QWidget *parent = 0);

protected:
   // /*
    void initializeGL();
    //void resizeGL(int w, int h);
    //void paintGL();
    void glutKeyboard (unsigned char key, int /*x*/, int /*y*/);
    void glutDisplay(void);
    void glutIdle (void);
    void CleanupExit();
    void LoadCalibration();
    void SaveCalibration();
  // */
signals:

public slots:

};

and this is my function glutKeyboard

    void VideoOpenGL::glutKeyboard (unsigned char key, int /*x*/, int /*y*/)
{
    switch (key)
    {
    case 27:
        CleanupExit();
    case 'b':
        // Draw background?
        g_bDrawBackground = !g_bDrawBackground;
        break;
    case 'x':
        // Draw pixels at all?
        g_bDrawPixels = !g_bDrawPixels;
        break;
    case 's':
        // Draw Skeleton?
        g_bDrawSkeleton = !g_bDrawSkeleton;
        break;
    case 'i':
        // Print label?
        g_bPrintID = !g_bPrintID;
        break;
    case 'l':
        // Print ID & state as label, or only ID?
        g_bPrintState = !g_bPrintState;
        break;
    case 'f':
        // Print FrameID
        g_bPrintFrameID = !g_bPrintFrameID;
        break;
    case 'j':
        // Mark joints
        g_bMarkJoints = !g_bMarkJoints;
        break;
    case'p':
        g_bPause = !g_bPause;
        break;
    case 'S':
        SaveCalibration();
        break;
    case 'L':
        LoadCalibration();
        break;
    }
}

and now the call to function

 glutKeyboardFunc( glutKeyboard );

Solution

  • glutKeyboardFunc() expects the specified callback to be a standalone function, but you are specifying a non-static class method instead, which is not compatible because of the hidden this parameter, which glut does not know about. Hence the error.

    You have three choices:

    1. Get rid of the VideoOpenGL class and make glutKeyboard() be a standalone function.

    2. Keep the VideoOpenGL class but declare glutKeyboard() as static to remove the this parameter. This does mean that glutKeyboard() will not be able to directly access non-static members of the VideoOpenGL class anymore. glutKeyboardFunc() does not allow you to pass a user-defined value to glutKeyboard() so you would need to declare your own global VideoOpenGL* pointer that points at a VideoOpenGL object, then you can access its non-static members via that pointer.

    3. Create a proxy thunk that implements a compatible interface for glutKeyboardFunc() to call, and have the thunk internally delegate its work to a VideoOpenGL object.