无法将 int {Class}::* 转换为 int*

Cannot convert int {Class}::* to int*

当我尝试调用一个函数时,将对我的类型为 int 的变量的引用传递给采用 int 类型的函数,我得到一个错误,似乎指出 int 是它声明的 class 类型,这是为什么?

Header:

class MyClass {
    public:
        int MAJOR = 3;
        int MINOR = 3;
        int REV = 0;
}

代码:

glfwGetVersion(&MyClass::OPENGL_VERSION_MAJOR, &MyClass::OPENGL_VERSION_MINOR, &MyClass::OPENGL_VERSION_REV);

错误:

error: cannot convert 'int MyClass::*' to 'int*' for argument '1' to 'void glfwGetVersion(int*, int*, int*)' 

更改为:

class MyClass {
    public:
        static const int MAJOR = 3;
        static const int MINOR = 3;
        static const int REV = 0;
};

如果这些版本不变


否则为:

class MyClass {
    public:
        static int MAJOR;
        static int MINOR;
        static int REV;
};

然后在 .cpp 文件中的某处

int MyClass::MAJOR = 3;
int MyClass::MINOR = 3;
int MyClass::REV = 0;

勾选live example here

&MyClass::OPENGL_VERSION_MAJOR 是成员指针。

您可以使用

MyClass instance;

glfwGetVersion(&instance.OPENGL_VERSION_MAJOR,
               &instance.OPENGL_VERSION_MINOR,
               &instance.OPENGL_VERSION_REV);