这是错误:
undefined reference to `Main::playerX'
有人可以解释为什么会出错吗?
main.cpp(主要功能):
#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){
}
可能它与静态变量playerX有关,但我不知道该怎么做。
答案 0 :(得分:2)
静态变量属于该类,而不属于该类的对象。
因此你必须像这样初始化它们(在源文件中):
...
Main::Main() {}
float Main::playerX = 0.0;
...