0

This is the error:

undefined reference to `Main::playerX'

Can someone explain why it gives the error?

main.cpp(main function):

#include <GL/glut.h>
#include "Input.h"
#include "Main.h"
Input inputClass;
Main mainClass;

int main (int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE);
    glutInitWindowSize (800, 600);
    glutInitWindowPosition (100, 100);
    glutCreateWindow ("Your first OpenGL Window");
    glutDisplayFunc(mainClass.display);
    glutMainLoop();
}

Input.h:

#ifndef INPUT_H
#define INPUT_H

class Input
{
    public:
        Input();
        static void SpecialDown(int key, int x, int y);
        static void returnSpeed(float speed);
};

#endif // INPUT_H

Input.cpp:

#include "Input.h"
#include <GL/glut.h>
#include <GL/glu.h>
#include "Main.h"
Main mainClass2;
Input::Input()
{
    //ctor
}

void Input::SpecialDown(int key, int x, int y){
    switch(key){
        case GLUT_KEY_UP:
            returnSpeed(0.0f);
            break;
        case GLUT_KEY_DOWN:
            returnSpeed(0.0f);
            break;
        case GLUT_KEY_LEFT:
            returnSpeed(-0.005f);
            break;
        case GLUT_KEY_RIGHT:
            returnSpeed(0.005f);
            break;
    }
    //glutPostRedisp();
}

void Input::returnSpeed(float speed){
    mainClass2.test(speed);
}

Main.h:

#ifndef MAIN_H
#define MAIN_H


class Main{
    public:
        Main();
        static void reshape(int width, int height);
        static void display();
        void changeSpeed(float speed);
        static float playerX;
        void test(float kip);
        static float playerSpeed;
};

#endif // MAIN_H

Main.cpp:

#include "Main.h"
#include "Input.h"
#include <GL/glut.h>
#include <GL/glu.h>
Main::Main(){
    playerX = 0.0f;
}
Input input;

///HERE EVERYTHING IS BEING RENDERED
void Main::display (){
    ///BACKGROUND COLOR
    glClearColor(0.3f, 0.3f, 2.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
    ///LOAD THE IDENTITY MATRIX FOR THE DRAWING
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -5.0f);

    ///THE GROUND
    glBegin(GL_QUADS);
        glColor3f(0.5f,2.0f,0.5f);
        glVertex2f(-5.0f,-4.0f);
        glVertex2f(-5.0f,-1.0f);
        glVertex2f(10.0f,-1.0f);
        glVertex2f(10.0f,-4.0f);
    glEnd();

    glTranslatef(playerX,0.0f,0.0f);
    glBegin(GL_QUADS);
        glColor3f(1.0f,0.0f,0.0f);
        glVertex2f(-3.0f,-1.0f);
        glVertex2f(-3.0f,-0.5f);
        glVertex2f(-2.5f,-0.5f);
        glVertex2f(-2.5f,-1.0f);
    glEnd();

    ///SWAP THE BUFFERS FOR SMOOTH RENDERING
    glutSwapBuffers();
    playerX += playerSpeed;
}

void Main::test(float kip){
}

Probably it has to do with the static variable playerX, but I don't know what to do.

genpfault
  • 51,148
  • 11
  • 85
  • 139
OpenGLmaster1992
  • 281
  • 2
  • 5
  • 13

1 Answers1

2

Static variables belong to the class not to the objects of that class.

Therefore you have to initialize them like so (in source file):

...
Main::Main() {}
float Main::playerX = 0.0;
...
Patryk
  • 22,602
  • 44
  • 128
  • 244