【问题标题】:glfw non-standard syntax; use '&' to create a pointer to memberglfw 非标准语法;使用 '&' 创建指向成员的指针
【发布时间】:2017-12-02 08:08:59
【问题描述】:

我不断收到non-standard syntax; use '&' to create a pointer to member

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

如果 Resize 函数是全局的,它就可以工作

小例子:

#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;
}

【问题讨论】:

  • 在本页顶部的搜索框中输入错误信息。
  • 我找不到任何与 gflw 回调相关的内容
  • s/window.Resize/&amp;window::Resize 并确保将Resize() 声明为static
  • @jeana glfw 与它几乎没有关系。这是一个更普遍的问题。
  • @user0042 a storage class may not be specified here 我将 resize 设为静态,window.setResize(&amp;window::Resize); 给出 :: must be a class or namespace name

标签: c++ glfw


【解决方案1】:

您的函数指针与传递给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;
}

【讨论】:

  • 谢谢你这个作品,你能解释为什么会这样吗?查看 GLFWwindowsizefun 的 typedef 并没有发现它需要是静态的,如果函数是全局的,它的工作方式不是静态的
  • 等等我现在还有一个问题,静态函数不能使用类变量
  • 看起来您需要专门化 GLFWwindow 类来创建 test 的实际实例。请参阅 GLFW 文档如何执行此操作。
  • 什么?为什么?这个洞的东西没有意义
  • @jeana 我不是 GLFW 专家,但看起来您正在与普通的 C-API 交互。您需要将GLFWwindow* 指针与您的测试类封装在一起,并将实例与glfwCreateWindow() 调用相关联。我怀疑这就是这些 NULLed 参数的用途。
猜你喜欢
  • 2020-03-30
  • 1970-01-01
  • 2021-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 1970-01-01
相关资源
最近更新 更多