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();
}
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.
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?