1

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

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
Chemistpp
  • 2,006
  • 2
  • 28
  • 48
  • possible duplicate of [Unresolved external symbol on static class members](http://stackoverflow.com/questions/195207/unresolved-external-symbol-on-static-class-members) – Oliver Charlesworth May 29 '13 at 14:13

1 Answers1

3

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;
}
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • Bingo! Keeping the decleration in the header, I added `float App::angle = 0.0f;` in the App.cpp just under the headers and it works perfect. Doesn't like redeclaring `static` though. – Chemistpp May 29 '13 at 14:20
  • 1
    Note that it is not necessary to qualify the static member with its class name. `angle` on its own works as well as `App::angle` in the method implementation (in this case, where there are no conflicting names). You still need the `App::` qualifier when *defining* the variable. – RobH May 29 '13 at 14:22