指向一个类成员的函数 - glfw setKeycallback [英] Pointing to a function that is a class member - glfw setKeycallback

查看:1872
本文介绍了指向一个类成员的函数 - glfw setKeycallback的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个glfw应用程序,其中我将函数callse包装到一个简单的类中。我有设置键回调的麻烦。
我的类被定义为:

I'm writing a glfw app, in which I've wrapped the function callse into a simple class. Im having trouble setting the key callback. My class is defined as:

class GAME 
{
private:
    bool running;
public:
    GAME();
    int execute();
    void events(int, int);
    int loop();
    int render();
};

执行函数为:

int GAME::execute() 
    {
        glfwOpenWindow(640, 320, 8, 8, 8, 8, 0, 0, GLFW_WINDOW);
        glfwSetWindowTitle("Viraj");
        glfwSetKeyCallback(events);
        running = true;
        while(glfwGetWindowParam(GLFW_OPENED))
        {
            glfwPollEvents();
            loop();
            render();
        }
        return 0;
    }

在Visual Studio 2010上编译以下代码时出现错误:

Compiling the following code on Visual Studio 2010, gives the error:

错误C3867:'GAME :: events':函数调用缺少参数列表;

error C3867: 'GAME::events': function call missing argument list; use '&GAME::events' to create a pointer to member

使用& GAME :: events可以创建指向成员的指针

Using &GAME::events gives:

错误C2664:'glfwSetKeyCallback':无法将参数1从'void(__thiscall GAME :: *)(int,int)'转换为'GLFWkeyfun'
1>没有可以进行这种转换的上下文。

error C2664: 'glfwSetKeyCallback' : cannot convert parameter 1 from 'void (__thiscall GAME::* )(int,int)' to 'GLFWkeyfun' 1> There is no context in which this conversion is possible

推荐答案

有一个C ++语法指向类成员方法,但是你不能将它们传递给C风格的API。 C理解函数调用和每个非静态对象方法,以你的事件为例,看起来像这样思考C: void events(void * this,int,int); 意味着除了标准参数之外的每个方法还会获得这个指针静默传递。

There is a C++ syntax for pointing to class member methods but you cannot pass them to a C style API. C understands function calls and every non-static object method, taking your events as an example, looks like this thinking in C terms: void events(void* this, int, int); meaning that every method apart from the standard arguments also gets a this pointer silently passed.

要使事件 C兼容,使它 static void events(int,int); 。这样,它将遵循C调用语义 - 它不需要这个指针得到传递。你还必须以某种方式将你的对象传递给这个回调函数(如果你在回调函数中需要这个对象的数据)。

To make your events C compatible make it static void events(int, int);. This way it will follow the C calling semantics - it will not require a this pointer getting passed. You have to also somehow pass your object to this callback in some other manner (if you need this object's data in the callback).

这篇关于指向一个类成员的函数 - glfw setKeycallback的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆