-2

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'|

anatolyg
  • 26,506
  • 9
  • 60
  • 134
JC2112
  • 77
  • 1
  • 1
  • 12
  • 2
    This is almost certainly a linking error. How do you build the executable? – Beta Oct 27 '13 at 19:06
  • Let's see some actual code, not these snippets. – AndyG Oct 27 '13 at 19:07
  • 1
    Downvote because: a) you did not post the actual compiler/linker error, and since you don't understand the problem you have also failed to provide key information. b) the code you posted is nowhere near to an [sscce](http://sscce.org/) so we're left grasping at straws for you. If I want to grasp at straws, I'll work on my own code, thanks. – kfsone Oct 27 '13 at 19:08
  • Thanks for updating, unfortunately the "asteroid.cpp" code you posted doesn't even remotely compile. You have `using namespace std; new Asteroid asteroid1; cout << "Program is running"; new Asteroid asteroid2;` at the top level outside of a function. `new Asteroid asteroid1` is not legal C++. How are you compiling the code? It seems like the problem might be that it's not being recompiled and hence it's not linking. – kfsone Oct 27 '13 at 19:47
  • I previously wrote an explanation of what a "compilation unit" is [here](http://stackoverflow.com/questions/18579340/header-guards-do-not-seem-to-work/18580233#18580233). Read that to make sure you understand the roles of .cpp and .h files. Then, consider boiling this down to a much simpler subset of code so you can get everything working and build on it in a manageable fashion. – kfsone Oct 27 '13 at 19:50
  • Thanks for posting the code! Please fix your indenting, as well as asteriod.h. Next, please post collision.cpp – AndyG Oct 27 '13 at 19:52

1 Answers1

0

The linker has to look at all your source files (*.cpp) in order to make an executable. So you have to give all the file names to the linker somehow. How exactly? That depends on your platform.

If you are using gcc:

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

If you are using MS Visual Studio:

Drop your files asteroid.cpp, collision.cpp and all header files onto the "project" or "solution" or whatever it is called.

If you use another system, specify it in your question, and someone who is familiar with it will help you.

anatolyg
  • 26,506
  • 9
  • 60
  • 134