"undefined reference" error when the reference is defined

Hi, can someone help me out? I've been plagued by this one problem all day and I don't know what to do about it.

I'm trying to break everything up into separate files because I want to be able to reuse some of the classes I'm creating, right? So, I have Color.h and Color.cpp, plus both files for Body and both files for Sphere which extends Body, and finally both files for Cinfo, which is basically a class solely used to pass lots of information between stuff at the same time so the program doesn't have to repeat the equations, which are kind of costly but give lots of information at once when they're done.

Cinfo has a constructor which looks like this:
1
2
3
4
5
Cinfo::Cinfo (float di, float ti, Color ci) {
	d = di;
	t = ti;
	c = ci;
}

I've overloaded the = operator for Color and tested that independently and I know it works.
Body has a function that looks like this:
1
2
3
Cinfo Body::collision(Vector3d c) {
	return Cinfo(-1, -1, Color(0, 0, 0));
}

Obviously this is not the final code for determining collisions, but I just want to make sure everything works first.
Finally, Sphere, which extends Body, has:
1
2
3
Cinfo Sphere::collision(Vector3d c) {
	return Cinfo(1, 1, Color(255, 255, 255));
}

So essentially all it should do right now when trying to check collisions is return two -1s and the color black on a default Body and two 1s and the color white if that Body is a Sphere in particular. However, when I try to compile, I get the following:
1
2
3
4
5
6
In function `Body::collision(Vector3d)':
Body.cpp:(.text+0x5a): undefined reference to `Cinfo::Cinfo(float, float, Color)'
classes/Sphere.o: In function `Sphere::collision(Vector3d)':
Sphere.cpp:(.text+0xc8): undefined reference to `Cinfo::Cinfo(float, float, Color)'
collect2: ld returned 1 exit status
make: *** [main] Error 1

Of course, my Body.h includes Cinfo.h, and of course Sphere includes it also by virtue of extending Body. I have triple-checked my spellings and capitalizations on all files and headers and function calls in both the .h's and the .cpp's. Any ideas?
If you're using an ide, make sure the cpp file in which the function is defined is included in the project.

Undefined reference to 'XXX' is linker-speak for "I can't find the implementation of that function."
You'll have to provide more of your code then. Specifically the body.cpp/.hpp and Cinfo.hpp files should be good.
Sure thing. Body.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#ifndef _H_BODY
#define _H_BODY

#include "Matrix.h"
#include "Shader.h"
#include "Cinfo.h"

using namespace std;

class Body {
	public:
	Shader* s;
	Body();
	Cinfo collision (Vector3d c);
};

#endif 

Cinfo.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef _H_CINFO
#define _H_CINFO

#include "Color.h"

using namespace std;

class Cinfo {
	public:
	float d;
	float t;
	Color c;
	Cinfo(float di = -1, float ti = -1, Color ci = Color());
};

#endif 

Body.cpp:
1
2
3
4
5
6
7
8
9
#include "Body.h"

using namespace std;

Body::Body() {}

Cinfo Body::collision(Vector3d c) {
	return Cinfo(-1, -1, Color(0, 0, 0));
}

and Cinfo.cpp:
1
2
3
4
5
6
7
8
9
#include "Cinfo.h"

using namespace std;

Cinfo::Cinfo (float di, float ti, Color ci) {
	d = di;
	t = ti;
	c = ci;
}


Like I said, a lot of it's empty because I'm still trying to get it set up, but I don't see how that would be the cause of my problem.

EDIT: just for clarity's sake, Vector3d is in Matrix.
Last edited on
One source file with the missing function in it... Make sure you've included it in your project. (Having it open in the editor does not mean it's included in your project.)
Aha! You're right, I derped and wrote classes/Cinfo.h where I should have written classes/Cinfo.o in one place in my makefile. Fixed it now! I have a bad case of the dumb. Thanks for your help and all the quick replies!
Topic archived. No new replies allowed.