无法转换' classname :: glutKeyboard' from type' void(classname ::)(unsigned char,int,int)'输入' void(*)(unsigned char,int,int)'

时间:2014-05-21 04:53:38

标签: c++ function glut

我用c ++创建了一个kinect应用程序,但我的glut函数中有相同的错误,void glutKeyboard,glutDisplay,glutIdle。 在下面的示例中,我在主文件中声明了所有函数,因此不需要类,但在我的应用程序中需要,但是类通过声明函数的范围来生成错误。

这和函数头的声明:

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:

};

这是我的函数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;
    }
}

现在调用函数

 glutKeyboardFunc( glutKeyboard );

2 个答案:

答案 0 :(得分:1)

glutKeyboardFunc()期望指定的回调是一个独立的函数,但是你要指定一个非静态类方法,由于隐藏的this参数,它不兼容,关于。因此错误。

您有三种选择:

  1. 摆脱VideoOpenGL课程,让glutKeyboard()成为一个独立的职能。

  2. 保留VideoOpenGL类,但将glutKeyboard()声明为static以删除this参数。这确实意味着glutKeyboard()将无法再直接访问VideoOpenGL类的非静态成员。 glutKeyboardFunc()不允许您将用户定义的值传递给glutKeyboard(),因此您需要声明一个指向VideoOpenGL*对象的全局VideoOpenGL指针,然后您可以通过该指针访问其非静态成员。

  3. 创建一个代理thunk,实现glutKeyboardFunc()调用的兼容接口,并让thunk在内部将其工作委托给VideoOpenGL对象。

答案 1 :(得分:0)

更改

void glutKeyboard (unsigned char key, int /*x*/, int /*y*/);

到静态成员函数,如果你想以你想要的方式使用它。

static void glutKeyboard (unsigned char key, int /*x*/, int /*y*/);
相关问题