C++中使用freeglut时出现“undefined reference to Base::object”链接错误

4
我有这段代码
/////////////////////////////////////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;
};

如果您需要实现,请告诉我。
最后是引擎类。
 #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 ();


};

//和 partial engine.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++;
 }
}

所以,当我尝试构建它时,链接器显示以下错误:未定义Living::spriteImg的引用。
2个回答

12

这是一个链接器错误,告诉你链接器找不到Living::spriteImg的定义,这是因为你只是在类中声明它,但没有定义

你需要在Living.cpp中定义这个静态成员变量:

Sprite* Living::spriteImg = NULL;

请记住,您需要在一个且仅有一个.cpp文件中定义它。

好的阅读材料:
什么是定义和声明之间的区别?


0

Living::SpriteImg是一个static数据成员。你只在class Living中声明了它。

然而,static数据成员也需要单独定义,最好放在一个.cpp文件中。

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

最好是用 .cpp 的文件格式,必须得是 cpp 文件。 - Alok Save
@Als,不对。如果给定的.h文件仅在1个转换单元中包含,则可以在同一.h文件中定义static成员。另一种情况:对于模板,static成员定义必须对所有转换单元可见,因此在这种情况下,定义大多包含在.h文件中!这就是为什么我使用preferably的原因。 - iammilind
在 OP 的情况下,没有“最好”的选择,只有“必须”的要求。OP 没有使用模板,也没有将“.h”文件包含在一个翻译单元中(在项目中,几乎不可能只有一个翻译单元包含“.h”文件,这是非常罕见的)。 - Alok Save

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接