0

I tried to make a simple program that will display a triangle in the window. I also wanted it to be in the class, not in the main function.

Here is my code:

#include <gl/glut.h>
#include <iostream>

class MainWindow {
    static MainWindow* currentInstance;

    void update() {
        glutDisplayFunc(renderCallback);
        glutKeyboardFunc(keyboardCallback);
        glutMouseFunc(mouseCallback);
    }

    //RENDER
    static void renderCallback() {
        currentInstance->render();
    }

    void render() {
        currentInstance = this;

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glBegin(GL_TRIANGLES);
        glColor3f(1, 0, 0);
        glVertex2f(-0.5, -0.5);
        glColor3f(0, 1, 0);
        glVertex2f(0.5, -0.5);
        glColor3f(0, 0, 1);
        glVertex2f(0.0, 0.5);

        glEnd();

        glutSwapBuffers();
    }

    //KEYBOARD
    static void keyboardCallback(unsigned char c, int x, int y) {
        currentInstance->keyboard(c, x, y);
    }

    void keyboard(unsigned char c, int x, int y) {
        if (c == 27) {
            glutDestroyWindow(glutGetWindow());
            exit(0);
        }
    }

    //MOUSE
    static void mouseCallback(int button, int state, int x, int y) {
        currentInstance->mouse(button, state, x, y);
    }

    void mouse(int button, int state, int x, int y) {
        if (button == GLUT_RIGHT_BUTTON) {
            glutDestroyWindow(glutGetWindow());
            exit(0);
        }
    }

public:
    MainWindow(int argc, char** argv) {
        glutInit(&argc, argv);
        glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
        glutInitWindowPosition(100, 100);
        glutInitWindowSize(640, 480);
        glutCreateWindow("Simple GLUT Application");

        update();

        glutMainLoop();
    }
};

int main(int argc, char** argv) {
    MainWindow window(argc, argv);
}

When I try to compile it I get Error:

LNK2001 unresolved external symbol "private: static class MainWindow * MainWindow::currentInstance" (?currentInstance@MainWindwo@@0PAV1@A).
genpfault
  • 51,148
  • 11
  • 85
  • 139
UwU
  • 11
  • This question has nothing to do with OpenGL. It is just a question about static attributes in c++. – Rabbid76 May 11 '21 at 19:38
  • See [Static data memberss](https://en.cppreference.com/w/cpp/language/static) – Rabbid76 May 11 '21 at 19:39
  • 1
    Related: [https://stackoverflow.com/questions/18749071/why-does-a-static-data-member-need-to-be-defined-outside-of-the-class](https://stackoverflow.com/questions/18749071/why-does-a-static-data-member-need-to-be-defined-outside-of-the-class) – drescherjm May 11 '21 at 19:40
  • Thank you guys so much, it is working now – UwU May 11 '21 at 19:54

0 Answers0