无法将'classname::glutKeyboard'从类型 'void (classname::)(unsigned char, int, int)' 转换为类型 'void (*)(u



我在c++中创建了一个kinect应用程序,但我在我的glut函数中有同样的错误,void glutKeyboard, glutDisplay, glutIdle。在下面的例子中,我是在主文件中声明的所有函数,所以不需要类,但在我的应用程序中需要,但是类通过声明函数的作用域产生错误。

this和函数头的声明:

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 );

glutKeyboardFunc()期望指定的回调是一个独立的函数,但是您指定的是一个非静态类方法,由于隐藏的this参数,这是不兼容的,这是glut不知道的。因此出现了错误。

你有三个选择:

  1. 去掉VideoOpenGL类,使glutKeyboard()成为独立函数

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

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

变化

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

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

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

最新更新