我目前正在学习C ++中的OpenGL。
我正在尝试通过设置glfwSetKeyCallback(this->window, ctrl->key_callback);
来读取键盘输入,其中this->window
是我的GLFWwindow* window
,而ctrl->key_callback
是我的自定义对象Controller
的方法
我收到MSVC的编译器错误:
非标准语法;使用“&”创建指向成员的指针
如何通过key_callback
指针指示Controller* ctrl
方法?
错误出现的地方:
void Class1::set_callback(Controller* ctrl)
{
glfwSetKeyCallback(this->window, ctrl->key_callback);
}
Controller.h
#include "Class1.h"
#include "GLFW/glfw3.h"
class Controller
{
public:
Controller(Class1* c);
~Controller();
void key_callback(GLFWwindow* glwindow, int key, int scancode, int action, int mods);
private:
Window* window;
};
我正在main.cpp中调用set_callback
#include "Class1.h"
#include "Controller.h"
int main()
{
Class1* my_class = new Class1();
Controller* controller = new Controller(my_class);
my_class->set_callback(controller);
return 0;
}
请让我知道,如果我没有正确提出我的问题/标题,我对这种语法很困惑
答案 0 :(得分:2)
不能。非静态成员函数具有此参数。 void key_callback(GLFWwindow* glwindow, int key, int scancode, int action, int mods)
的签名实际上是void key_callback(Controller*this, GLFWwindow* glwindow, int key, int scancode, int action, int mods)
。因此,它根本无法满足glfwSetKeyCallback
的要求。
我建议您使用thunk函数(独立函数或静态成员函数):
void key_callback_thunk(GLFWwindow* glwindow, int key, int scancode, int action, int mods) {
auto self = static_cast<Controller*>(glfwGetWindowUserPointer(glwindow));
self->key_callback(glwindow, key, scancode, action, mods);
}
void Class1::set_callback(Controller* ctrl)
{
glfwSetWindowUserPointer(this->window, ctrl);
glfwSetKeyCallback(this->window, key_callback_thunk);
}
请注意,glfwSetWindowUserPointer
只能为给定窗口存储一个指针。再次调用它,该值将被覆盖。
答案 1 :(得分:2)
您不能具有用于回调的成员函数,glfwSetKeyCallback
使用一个指向自由函数的指针。它的签名是
df %>%
ggplot(aes(question,perc,fill=answer))+geom_tile()+
geom_text(aes(label=answer))
GLFWkeyfun glfwSetKeyCallback(GLFWwindow* window, GLFWkeyfun cbfun);
在哪里
GLFWkeyfun