2011-09-03 173 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

@如果此答案回答了您的問題,請不要忘記將其標記爲答案,方法是單擊左側的複選標記。 –