2012-01-31 3 views
3

가이 코드를정의되지 않은 참조/freeglut

///////////////////////// ////////////Gnome.cpp 파일

#include "Living.h" 

class Gnome:public Living{ 
private: 

public: 
Gnome(); 
void drawObjects(); 
}; 


Gnome::Gnome() 
{ 
spriteImg = new Sprite("graphics/gnome.bmp"); //<-------------this is where it says there is the error 

loaded = true; 
} 

/////////////////////////// ////////Living.h 파일

#include <iostream> 

#include "Sprite.h" 


using namespace std; 

class Sprite; 

class Living{ 

protected: 
int x,y; 
static Sprite *spriteImg; //= NULL; 
bool loaded; 


void reset(); 


public: 

Living(); 
~Living(); 
int getX(); 
void setX(int _x); 
int getY(); 
void setY(int _y); 
void virtual drawObjects() =0; 


}; 

// living.cpp

#include "Living.h" 


Living::Living() 
{ 
x=y=0; 
reset(); 
} 


Living::~Living() 
{ 
delete spriteImg; 

} 

void Living::setX(int _x) 
{ 
x=_x; 
} 

int Living::getX() 
{ 
return x; 
} 


void Living::setY(int _y) 
{ 
y=_y; 
} 

int Living::getY() 
{ 
return y; 
} 


void Living::reset() 
{ 
spriteImg = NULL; 
loaded = false; 
} 

// sprite.h

#include <GL/glut.h> 

#include <string> 

using namespace std; 

class ImageLoader; 

class Sprite 
{ 
public: 
/** 
* Enable 2D drawing mode to draw our sprites. This function MUST be called before 
* any sprite is drawn on screen using the draw method. 
*/ 
static void enable2D(); 

/** 
* Disables the 2D drawing. This can be called before you are done drawing all 2D 
* sprites and want to draw 3D now. 
*/ 
static void disable2D(); 

Sprite(string filename); 
virtual ~Sprite(); 

virtual void draw(); 
virtual void drawLiving(int dir, int flag); //flag=1 move 
virtual void draw(int X, int Y); 
virtual void rotate(GLint degrees); 

// getter and setter methods 
GLint getAngle() const; 
void setAngle(GLint degrees); 
void setX(GLdouble x); 
void setY(GLdouble y); 
GLint getHeight() const; 
GLint getWidth() const; 

/** 
* Sets the pivot point in relation to the sprite itself, that is using the object 
* coordiantes system. In this coordiantes system the bottom left point of the object 
* is at (0, 0) and the top right is at (1, 1). 
* 
* E.g. to set the pivot to be in the middle of the sprite use (0.5, 0.5) 
* Default values are (1, 1). 
* @param pivotX Can be any value, but when x is in the range [0, 1] the pivot is inside the 
* sprite where 0 is the left edge of the sprite and 1 is the right edge of the sprite. 
* @param pivotY Can be any value, but when y is in the range [0, 1] the pivot is inside the 
* sprite where 0 is the bottom edge of the sprite and 1 is the top edge of the sprite. 
*/ 
void setPivot(GLfloat pivotX, GLfloat pivotY); 
GLfloat getPivotX() const; 
GLfloat getPivotY() const; 

GLdouble getX() const; 
GLdouble getY() const; 

/** 
* Sets the pivot to be at the point where object's pivot is set. 
* @param obj The reference object to whose pivot we will set this pivot to be. 
* Note: if the obj pivot changes or the obj moves after the setPivot call has 
* been issued, the pivot of this object will not reflect this changes. You must 
* call setPivot again with that object to update the pivot information. 
*/ 
void setPivot(const Sprite &obj); 

/** 
* Sets the scale of the object. A scale of (1.0, 1.0) means the sprite 
* maintains its original size. Values larger than 1 scale the sprite up 
* while values less than 1 shrink it down. 
*/ 
void setScale(GLfloat x, GLfloat y); 
private: 
ImageLoader *image; 
GLuint textureID; 
GLint angle; 
GLdouble x; 
GLdouble y; 
GLfloat pivotX; 
GLfloat pivotY; 
GLfloat scaleX; 
GLfloat scaleY; 
GLint step; 
//GLint dir; 

//----------------------------------------------------------------------------- 
// Initializes extensions, textures, render states, etc. before rendering 
//----------------------------------------------------------------------------- 
void initScene(); 

/** 
* A helper function taken from http://www.opengl.org/resources/features/OGLextensions/ 
* to help determine if an OpenGL extension is supported on the target machine at run-time 
* @param extension The extension name as a string. 
* @return True if extension is supported and false if it is not. 
*/ 
bool isExtensionSupported(const char *extension) const; 
}; 

// 당신은 PLZ 알려 구현을 필요로하는 경우

// 마지막으로 엔진 클래스

#include <conio.h> 

#include "Living.h" 

#include "Gnome.h" 



using namespace std; 
class Engine{ 


private: 
vector<char*> gamemap; 
int score; 
vector <Living*> gameobjs; 
void FindRandomPos(int &x_pos,int &y_pos); 
int mapMaxX; 
int mapMaxY; 


void collisionDetecion(int pX,int pY); 



public: 


Engine(); 
void init(const char* mapname="defaultmap.txt",int gnomes=1,int traals=1,int diamonds=10); 
void printMap(); 
void printMap2(); 
void movePotter(); 
void calculateScroll(); 
int getScore(); 
int getMapMaxX(); 
int getMapMaxY(); 


}; 

//와 부분적인 엔진. 나는 그것을 빌드 할 때 cpp에

void Engine::init(const char* mapname,int gnomes,int traals,int diamonds) 
{ 
int i=0; 
// int z=0; 
mapMaxX=0; 
mapMaxY=0; 





int x_pos,y_pos; 

int countobjs=0; 


//gnomes 
for(int i=0;i<gnomes;i++) 
{ 
FindRandomPos(x_pos,y_pos); 
gameobjs.push_back(new Gnome); //<---------------here is where the error should begin i think 
gameobjs[countobjs]->setX(x_pos); 
gameobjs[countobjs]->setY(y_pos); 
gamemap[gameobjs[countobjs]->getX()][gameobjs[countobjs]->getY()]='3'; 
countobjs++; 
} 
} 

때문에, 링커는이 오류를 보여줍니다 U를 생활에 ndefined 참조 : spriteImg

답변

6
그냥 클래스에를 선언하지만을 정의하지 때문에, 이것은 링커가 static 회원 Living::spriteImg에 대한 정의를 찾을 수 있음을 알려주는 링커 오류입니다

그것.

Sprite* Living::spriteImg = NULL; 

당신 하나만 .cpp 파일을 정의 할 필요가 있음을 기억

당신은 당신의 Living.cpp의 정적 멤버를 정의하는 네드.

좋은 읽기 :
What is the difference between a definition and a declaration?

0

Living::SpriteImgstatic 데이터 멤버입니다. class Living에서만 신고하셨습니다.

그러나 데이터 구성원도 .cpp 파일로 별도로 정의해야합니다.

// .cpp file 
Sprite* Living::spriteImg = 0; 
+0

* .cpp * 인 것이 바람직하고, 아니요 *는 cpp *이어야합니다. –

+0

@Als, 잘못되었습니다. 주어진'.h' 파일이 1 번역 단위에만 포함되어 있다면, 같은'.h' 파일 안에'static' 멤버를 정의 할 수 있습니다. 또 다른 시나리오는'template's의 경우'static' 멤버 정의가 모든 번역 단위에서 볼 수 있어야하므로이 경우 정의는 대부분'.h' 파일에 포함됩니다! 그래서 * 선호 *를 사용했습니다. – iammilind

+0

OP의 시나리오에서는 * 단지 *가 * 반드시 * 있어야합니다. OP는 템플릿을 사용하지 않습니다.'.h' 파일은 단지 하나의 번역 단위에 포함되어 있지 않습니다 (프로젝트에서 TU에만'.h' 파일이 포함될 가능성은 거의 없습니다 * 거의 없습니다 *). –