user1763295
user1763295

Reputation: 1088

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

When I try to call a function, passing in a reference to my variable which has the type int, to the function which takes a type of int, I get an error that seems to point out that the int is the type of the class it was declared in, why is this?

Header:

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

Code:

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

Error:

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

Upvotes: 1

Views: 2908

Answers (2)

Mohit Jain
Mohit Jain

Reputation: 30489

Change to:

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

If these versions are constant.


Otherwise as:

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

Then somewhere in a .cpp file

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

Check live example here

Upvotes: 4

Jarod42
Jarod42

Reputation: 217265

&MyClass::OPENGL_VERSION_MAJOR is a pointer on member.

You may use

MyClass instance;

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

Upvotes: 3

Related Questions