Undefined reference but it is defined.

I made this snippet to test whether or not the pointer would be destroyed when the A::add method goes out of scope. But when I tried to compile I got these errors:

obj\Debug\main.o||In function `main':|
|7|undefined reference to `A::A()'|
|10|undefined reference to `A::add(B*)'|
|11|undefined reference to `A::soundOff()'|
||=== Build finished: 3 errors, 0 warnings ===|


Here is my code:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include "A.h"
#include "B.h"

int main()
{
	A obj;
	B b(1);
	B* bpnt = &b;
	obj.add(bpnt);
	obj.soundOff();

	std::cin.get();
}


A.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef A_H
#define A_H
#include "B.h"
#include <vector>
#include <iostream>

class A
{
public:
	A();
	void add(B*);
	void soundOff();
private:
	std::vector<B*> bVect;
};
#endif 


A.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "A.h"

A::A()
{
	std::cout << "A created.";
}

void A::add(B* b)
{
	bVect.push_back(b);
}

void A::soundOff()
{
	for (unsigned int i = 0; i < bVect.size(); i++)
	{
		bVect[i]->talk();
	}
}


B.h
1
2
3
4
5
6
7
8
9
10
11
12
13
#ifndef B_H
#define B_H
#include <iostream>

class B
{
public:
	B(int);
	void talk();
private:
	int id;
};
#endif 


B.cpp
1
2
3
4
5
6
7
8
9
10
11
#include "B.h"

B::B(int i)
{
	id = i;
}

void B::talk()
{
	std::cout << "ID: " << id;
}


It looks to me that I have A.h included in main.cpp and the constructor and methods are defined in A.h. So what is causing this?
Did you compile and link all the .cpp files?
I'm new to this. I don't know what you mean by your question. I'm using code::blocks with the GNU GCC Compiler. I'm not sure if that answers your question.

I thought code::blocks does all the compiling and linking for me when I tell it to build.
Also, all the header and cpp files are selected as build targets.
Last edited on
I thought so too. Are all the files in the same project?
Yes they are, but just to make sure, I'm going to delete this project and make a new one.

EDIT:

Strange, I made a new empty project and copy and pasted the code into new files. It worked this time.

I suspect this might have been caused somehow due to the fact that the original project was actually an old project that I gutted (deleted all the header and cpp files) and I added this code in without make a brand new project.

Thanks for the idea to make a new project.

Unrelated to the original problem, do you know if this code snippet would keep a pointer in the vector even after the add method goes out of scope?

I ran it and it seems that it does keep the pointer. I'm wondering why. The pointer I passed into the add method die when it goes out of scope, so I am guessing that when that pointer got passed into the push_back method, a clone of the pointer was made? When does the pointer inside the vector die?
Last edited on
Yes the vector will store a copy of the pointer. The pointer will "die" when you delete it or the vector is destructed.
Topic archived. No new replies allowed.