2011-09-03 137 views
0

我还是一种新的C++,不知道为什么我在尝试在另一个类中调用这些函数时遇到这些链接器错误。C++链接器错误LNK2019

的错误是:

error LNK2019: unresolved external symbol "public: float __thiscall Star::getMass(void)" ([email protected]@@QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" ([email protected]@@[email protected]@[email protected]) 

error LNK2019: unresolved external symbol "public: float __thiscall Star::getX(void)" ([email protected]@@QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" ([email protected]@@[email protected]@[email protected]) 

error LNK2019: unresolved external symbol "public: float __thiscall Star::getY(void)" ([email protected]@@QAEMXZ) referenced in function "public: void __thiscall Projectile::Update(class Star * const,int)" ([email protected]@@[email protected]@[email protected]) 

Projectile.cpp:

#include <hge.h> 
#include "Projectile.h" 
#include "Physics.h" 
#include "Star.h" 
#include <math.h> 

Projectile::Projectile(float xV, float yV, float x, float y, float m, HTEXTURE tex) 
{ 
    xVel = xV; 
    yVel = yV; 
    xPos = x; 
    yPos = y; 
    mass = m; 
    quad.tex = tex; 
} 

void Projectile::Update(Star stars[], int length) 
{ 
    for(int i = 0; i<length; ++i) 
    { 
     float force = Physics::calcGravityForce(mass, stars[i].getMass(), Physics::calcDist(xPos, yPos, stars[i].getX(), stars[i].getY())); 
     Accelerate(force, stars[i].getX() - xPos, stars[i].getY() - yPos); 
    } 
} 

void Projectile::Accelerate(float force, float x, float y) 
{ 
    float c = sqrt((x * x) + (y * y)); 
    xVel += x/c; 
    yVel += y/c; 
} 

星在Star.h定义在这里:

#ifndef STAR_H 
#define STAR_H 

#include <hge.h> 

class Star 
{ 
private: 
    float mass, radius, x, y; 
    hgeQuad quad; 

public: 
    Star(float m, float r, float X, float Y, HTEXTURE); 
    float getMass(); 
    float getRadius(); 
    float getX(); 
    float getY(); 
    Star() {} 
}; 

#endif 

回答

3

您已在Star宣布多项功能等级:

Star(float m, float r, float X, float Y, HTEXTURE); 
float getMass(); 
float getRadius(); 
float getX(); 
float getY(); 

而你正在尝试使用其中的一些而没有提供定义,也就是说,函数的主体,这就是为什么你会得到这些链接器错误。

一个新的.cpp文件添加到您的项目名为Star.cpp(名称并不重要,虽然),并添加的功能定义为Star类,就像您为Projectile类完成。 (您可以将它们添加到项目中的任何.cpp文件中,例如Projectile.cpp,但是如果您有单独的头文件,则最好还有一个单独的.cpp文件。)

或者如果您不想要到项目中有另一个CPP文件,你可以把职能的机构类本身内部:

class Star 
{ 
private: 
    float mass, radius, x, y; 
    hgeQuad quad; 

public: 
    Star(float m, float r, float X, float Y, HTEXTURE); 
    float getMass() { return mass; } 
    float getRadius() { return radius; } 
    float getX() { return x; } 
    float getY() { return y; } 
    Star() {} 
}; 

这风格是小常见的“吸气剂”的功能,如getMassgetRadius等刚刚返回成员变量。


虽然它不直接关系到你的问题,我要指出的几件事情:

  1. 让所有的“吸气剂”的功能(如getMass等)const(这样他们可以float getMass() const { return mass; }

  2. 因为你有成员variabl:通过将字const的参数(()在这种情况下)这样的后const Star对象)使用ES在Star类,你应该将它们设置为在没有参数的构造函数一些合理的默认值,

这样的:

Star() : mass(0), radius(0), x(0), y(0) {} 

将设置massradiusxy0。 (这种不寻常的语法称为初始化列表。您可以阅读关于它们的文章here。)

你甚至可以做到这一点没有一个单独的构造函数使用默认参数:

Star(float m = 0, float r = 0, float X = 0, float Y = 0, HTEXTURE = 0); 
+0

哇哦,我笨我完全忘了定义这些。对不起,感谢您的帮助! – Nate

+0

@如果此答案回答了您的问题,请不要忘记将其标记为答案,方法是单击左侧的复选标记。 –