Question

Class Header:

#ifndef _APP_H_
#define _APP_H_

#include "glut.h"
#include "Declare.h"


class App {
private:
    static float angle; 

public:

    App();
    int OnExecute();
    void OnLoop();
    static void OnRender();
    bool OnInit();
    void OnCleanup();
    static void OnResize(int w, int h);


};


#endif

My definition of OnRender is

#include "App.h"

App::App() {
    angle = 0.0f;
}

void App::OnRender() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    gluLookAt( 0.0f, 0.0f, 10.0f,
               0.0f, 0.0f, 0.0f,
               0.0f, 1.0f, 0.0f);
    glRotatef(angle, 0.0f, 1.0f, 0.0f);

    glBegin(GL_TRIANGLES);
        glVertex3f(-2.0f,-2.0f,0.0f);
        glVertex3f(2.0f,0.0f,0.0f);
        glVertex3f(0.0f,2.0f,0.0f);
    glEnd();

    angle+=0.1f;

    glutSwapBuffers();
}

Error:

1>App.obj : error LNK2001: unresolved external symbol "private: static float App::angle" (?angle@App@@0MA)
1>App_OnRender.obj : error LNK2019: unresolved external symbol "private: static float App::angle" (?angle@App@@0MA) referenced in function "public: static void __cdecl App::OnRender(void)" (?OnRender@App@@SAXXZ)

Something to do with how I am referencing the static variable inside the static function. If I don't declare angle as static float angle then I certainly can't access it via static void OnRender(). I have to add more details. If I don't declare it as static, I get this error illegal reference to non-static member App::angle

Was it helpful?

Solution

In the source file App.cpp you need to define your static variable:

static float App::angle = 0; //0 is my test default value. use yours

and if you want to use angle in a non static method, you may want to use the class instance with App:angle. For instance :

App::App() {
    App::angle = 0.0f;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top