In a header file, "asteroid.h", I have the struct 'Asteroid'. In the struct, I declare the following function.
bool isCollidingWith (Asteroid a2);
In the cpp file, "asteroid.cpp", I have the following code
bool Asteroid::isCollidingWith (Asteroid a2)
{
return true;
}
The program gives me the error
undefined reference to 'Asteroid::isCollidingWith(Asteroid)'
I've tried removing the "Asteroid::" from the .cpp, to no avail. In another .cpp, the main file (collision.cpp, which I cannot edit), calls "isCollidingWith"
if (a1.isCollidingWith(a2))
The above line is where my program pinpoints the error. collision.cpp does #include "asteroid.h", but not asteroid.cpp. In asteroid.cpp, I #include asteroid.h
, so I don't think my inclusions are in error. I looked online as to the common cause of the "undefined reference" error, and it's usually due to parameters in the .h file and the .cpp file being different, but mine are identical. Does anyone know what I can do to fix this?
Edit: Code for asteroid 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;
}
Code for 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
I realize that the "inside" of my functions in the cpp are basically empty; right now, I'm trying to just get the program to compile so that I can work on each function one at a time, but the error is preventing me. Here's the build message.
C:\Users\jclary\Desktop\adt_asteroids2\collision.cpp|44|undefined reference to `Asteroid::isCollidingWith(Asteroid) const'|