I was doing that exercise Graduation http://www.cplusplus.com/forum/articles/12974/ and a couple days ago i left my code. I came back today and when i tested it out, i get undefined reference to all my class' methods in main at Bunny obj; Im not sure if i typo something before saving and leaving, but i figured having more eyes on it, would find the error. To me is looks fine.
#ifndef CONTROL_H
#define CONTROL_H
#include <vector>
#include "../include/Bunny.h"
//using namespace std;
class Control{
private:
//vector bunny obj list
std::vector<Bunny> bunny_obj_list;
int bunny_count, loop_count, age_to_die, RA_number, total_old_death_count;
int total_RA_death_count, total_birth_count, female_count, starvation;
public:
Control();
~Control();
//get_bunny_obj_list = [] #list of bunny objects
int get_bunny_count();
int get_loop_count();
int get_age_to_die();
int get_RA_number();
int get_total_old_death_count();
int get_total_RA_death_count();
int get_total_birth_count();
int get_female_count();
int get_starvation();
};
#endif // CONTROL_H
#include "../include/Control.h"
#include "../inlcude/Bunny.h"
#include <vector>
Control::Control(){
//self.bunny_obj_list = [] #list of bunny objects
int bunny_count = 0; //bunny object count
int loop_count = 2012; // year
int age_to_die = 9; // +1
int RA_number = 0; //number of bunnies with radiation
int total_old_death_count = 0;
int total_RA_death_count = 0;
int total_birth_count = 0;
int female_count = 0;
int starvation = 0;
}
Control::~Control()
{
//dtor
}
and the error:
1 2 3 4 5 6 7 8 9 10 11
In file included from main.cpp:2:0:
include/Bunny.h:4:18: warning: extra tokens at end of #include directive [enabled by default]
/tmp/ccUmK9Fp.o: In function `main':
main.cpp:(.text+0x2e): undefined reference to `Bunny::Bunny()'
main.cpp:(.text+0x41): undefined reference to `Bunny::get_sex()'
main.cpp:(.text+0x7e): undefined reference to `Bunny::get_color()'
main.cpp:(.text+0xbb): undefined reference to `Bunny::get_name()'
main.cpp:(.text+0xf1): undefined reference to `Bunny::get_RA()'
main.cpp:(.text+0x128): undefined reference to `Bunny::~Bunny()'
main.cpp:(.text+0x189): undefined reference to `Bunny::~Bunny()'
collect2: ld returned 1 exit status
oh i figured it out. There was no problem with the code, it was the IDE somehow lost the headers from the project even though they still were in that directory? I dont know why that would affect it.
Glad you figured it out.
If the IDE doesn't include the header files, your cpp file goes "what Bunny are you talking about?", because the class's definition is in the header file.
why would the IDE need to know about the header file, if the code says to include it?
My only guess (not sure if i am right or not) is that it behind the scenes makes it own makefile, that it uses to compile everything and know its dependencies. (which that would make sense i guess).
It kind of re-enforces my idea that i like Vim editor better and make my own makefiles, if the IDE is somehow going to lose the headers every time i close out the program. But the makefile is such a pain, where the IDE does it for you, except for like now, it somehow forgets its own makefile (so to speak).
I believe that is indeed how most IDE's do it, make their own makefile (or project).
They shouldn't just "loose" header files however, it's probably a small glitch in the IDE he's using.
If you don't like makefiles, perhaps you consider trying "cmake", I find it a bit easier to maintain.
As a bonus, cmake will generate build files for a lot of platforms, including windows, so it's great for portable code.
The IDE I was using was code blocks. However I have been having some weird things happen in some distros, like my ubuntu code blocks acts weird (which is the distro i was using that i had this problem), but my Gentoo code blocks works fine.
I also am more comfortable with vim, and an IDE just doesnt feel right. So i'll check out cmake at some point, thanks.