在头文件“asteroid.h”中,我有结构“Asteroid”。在结构中,我声明了以下函数。
bool isCollidingWith (Asteroid a2);
在 cpp 文件“asteroid.cpp”中,我有以下代码
bool Asteroid::isCollidingWith (Asteroid a2)
{
return true;
}
该程序给了我错误
未定义对“Asteroid::isCollidingWith(Asteroid)”的引用
我尝试从 .cpp 中删除“Asteroid::”,但无济于事。在另一个 .cpp 中,主文件(collision.cpp,我无法编辑)调用“isCollidingWith”
if (a1.isCollidingWith(a2))
上面的行是我的程序查明错误的地方。碰撞.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'的引用|