2013-10-27 49 views
-2

在頭文件「asteroid.h」中,我有結構「Asteroid」。在結構中,我聲明瞭以下函數。對參數相同的成員函數的未定義引用

bool isCollidingWith (Asteroid a2); 

在cpp文件, 「asteroid.cpp」,我有以下的代碼

bool Asteroid::isCollidingWith (Asteroid a2) 
{ 
    return true; 
} 

節目給我的錯誤

未定義參考「小行星:: isCollidingWith (小行星)'

我試着從.cpp中刪除「Asteroid ::」,以 徒勞無功。在另一個的.cpp,主文件(collision.cpp,我不能編輯),所謂的「isCollidingWith」

if (a1.isCollidingWith(a2)) 

上面一行是我的程序查明的錯誤。 collision.cpp做#include「asteroid.h」,但不是asteroid.cpp。在asteroid.cpp中,I #include asteroid.h,所以我不認爲我的內含物是錯誤的。我在網上查找「未定義參考」錯誤的常見原因,通常是由於.h文件和.cpp文件中的參數不同,但我的參數是相同的。有誰知道我能做些什麼來解決這個問題?

編輯: 代碼小行星CPP

#include "asteroid.h" 
#include "point.h" 
#include "line.h" 
#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 
new Asteroid asteroid1; 
cout << "Program is running"; 
new Asteroid asteroid2; 

string Asteroid::getInput (Asteroid a) 
{ 
    cout << "whats the amount of vertices" 
    double input; 
    getline(cin, input); 
    a.amountofpoints = input; 
    input; 
    double xinput; 
    double yinput; 
    cout << "input two numbers for the x and y of each vertice" 
    while (input > 0) 
    { 
     getline(cin, input2); 
     getline(cin, input3); 
     a.vertice[input].x = input2; 
     a.vertice[input].y = input3; 
     input--; 
    } 
} 
bool Asteroid::isCollidingWith (Asteroid a2) const 
{ 
    //might need to take in Asteroid a2 to see if they're equal, not sure 
    return true; 
} 
Point Asteroid::getCenter() const 
{ 
    return a1; 
} 
double Asteroid::moveBy(double deltx, double delty) const 
{ 
    return deltx; 
    return delty; 
} 
void Asteroid::print() const 
{ 
    cout << "Print Function" << endl; 
    return a1; 
} 
Point Asteroid::addVertex(Point newvertex) const 
{ 
    return newvertex; 
} 

代碼asteroid.h

#ifndef ASTEROID_H_INCLUDED 
#define ASTEROID_H_INCLUDED 
#include <iostream> 
#include <string> 
#include "point.h" 
#include "line.h" 
using namespace std; 
struct Asteroid 
{ 
    int amountofpoints; 

    Point vertice; 
    /** 
    Create a line signment iwth the given endpoints. 
    */ 
    bool isCollidingWith (Asteroid a2) const; 
    Point getCenter() const; 
    string getInput (Asteroid a); 
    double moveBy (double deltx, double delty); 
    std::ostream& print(std::ostream &out); 
    Point addVertex(Point newvertex); 
    //bool isCollidingWith; 
}; 
#endif 

我意識到,我在CPP功能 「裏面的」 基本上都是空的;現在,我試圖讓程序編譯,以便我可以一次處理每個函數,但是錯誤阻止了我。這是構建消息。

C:\ Users \ jclary \ Desktop \ adt_asteroids2 \ collision.cpp | 44 |對Asteroid :: isCollidingWith(Asteroid)的未定義引用const |

+2

這幾乎肯定是一個鏈接錯誤。你如何構建可執行文件? – Beta

+0

讓我們看看一些實際的代碼,而不是這些代碼片段。 – AndyG

+1

Downvote是因爲:a)您沒有發佈實際的編譯器/鏈接器錯誤,並且由於您不瞭解問題,所以也未能提供關鍵信息。 b)您發佈的代碼遠不及[sscce](http://sscce.org/),因此我們將爲您抓緊吸管。如果我想抓住吸管,我會在自己的代碼上工作,謝謝。 – kfsone

回答

0

鏈接器必須查看所有源文件(* .cpp)才能生成可執行文件。所以你必須以某種方式給鏈接器提供所有的文件名。究竟如何?這取決於你的平臺。

如果你正在使用gcc:

g++ asteroid.cpp collision.cpp -o whatever 

如果你正在使用微軟的Visual Studio的:

您的文件拖放asteroid.cppcollision.cpp和所有的頭文件到 「項目」 或 「解決方案」或者無論它被稱爲什麼。

如果您使用其他系統,請在您的問題中指定它,熟悉它的人會幫助您。

相關問題