在頭文件「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 |
這幾乎肯定是一個鏈接錯誤。你如何構建可執行文件? – Beta
讓我們看看一些實際的代碼,而不是這些代碼片段。 – AndyG
Downvote是因爲:a)您沒有發佈實際的編譯器/鏈接器錯誤,並且由於您不瞭解問題,所以也未能提供關鍵信息。 b)您發佈的代碼遠不及[sscce](http://sscce.org/),因此我們將爲您抓緊吸管。如果我想抓住吸管,我會在自己的代碼上工作,謝謝。 – kfsone