在头文件“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