GLFW 非标准语法;使用 '&' 创建指向成员的指针

glfw non-standard syntax; use '&' to create a pointer to member

本文关键字:成员 指针 创建 使用 非标准 GLFW 语法      更新时间:2023-10-16

我一直得到non-standard syntax; use '&' to create a pointer to member

我不知道为什么它不起作用。

如果调整大小函数为全局

,它有效

最小示例:

#include <GLFW/glfw3.h>
class test {
public:
    test(){}
    ~test(){}
    void resize(GLFWwindow* window, int new_width, int new_height) {}
}resizer;
int main(){
    auto newwindow = glfwCreateWindow(1, 1, "test", NULL, NULL);
    glfwSetWindowSizeCallback(newwindow, resizer.resize);
   return 0;
}

最初的问题使用static作为函数解决了有点解决,但是这创建了我想在这里做的错误是简化的问题:

//rough replication of lib functions cannot change these
typedef void(* windowsizefun)(int,int);
void setWindowSizeCallback(windowsizefun fun){}
//the problem
class windowhandler{
    private:
        int width, height;
        static void resize(int new_width, int new_height) {
            width =new_width; height =new_height; //error
        }
    public:
        test(){
            width =100; height =100;
            setWindowSizeCallback(windowhandler::resize);
        }            
}
int main(){
    windowhandler newWindow();
    return 0;
}

您的功能指针与传递到glfwSetWindowSizeCallback()所需的声明不匹配。

它必须是static函数,并且需要正确应用范围操作员::

#include <GLFW/glfw3.h>
class test {
public:
    test(){}
    ~test(){}
    static void resize(GLFWwindow* window, int new_width, int new_height) {}
 // ^^^^^^
}  /*resizer*/;
// ^^^^^^^^^^^ No need for this.
int main(){
    auto newwindow = glfwCreateWindow(1, 1, "test", NULL, NULL);
    glfwSetWindowSizeCallback(newwindow, test::resize);
   return 0;
}