undefinded reference to my class

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.

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include "include/Bunny.h"
#include "include/Control.h"
#include <iostream>
//#include <ctime>
#include <cstdlib>
using namespace std;

float VERSION = 0.1;

int main(){
    srand(time(0));

    for(int i=0;i<100;i++){
        Bunny obj;
        cout << obj.get_sex() << endl;
        cout << obj.get_color() << endl;
        cout << obj.get_name() << endl;
        cout << obj.get_RA() << endl;
        cout << endl;
    }
}



the tree is:

1
2
3
4
5
6
7
8
/bunnies
main.cpp
  /include
      Bunny.h
      Control.h
  /src
      Bunny.cpp
      Control.cpp



EDIT: reason: added dependencies

Bunny.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#ifndef BUNNY_H
#define BUNNY_H

#include <string>;



class Bunny{
    private:
        std::string sex;
        std::string color;
        int age;
        std::string name;
        bool RA; //radioactive
    public:
        Bunny();
        ~Bunny();

        std::string get_sex();
        std::string set_sex();

        std::string get_color();
        std::string set_color();

        int get_age();
        //int set_age();

        std::string get_name();
        std::string set_name();

        bool get_RA();
        bool is_RA();

        int randnum(int max);

};

#endif // BUNNY_H



Bunny.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include "../include/Bunny.h"
#include <cstdlib>

using namespace std;

Bunny::Bunny(){
    sex = set_sex();
    color = set_color();
    name = set_name();
    age = 0;
    RA = is_RA();
}

Bunny::~Bunny(){

}



int Bunny::randnum(int max){
    //srand called in main
    int num = 0;
    num = (rand() % max);
    return num;
}



string Bunny::set_sex(){
    string sex[2] = {"Male", "Female"};
    return sex[randnum(2)];
}

string Bunny::get_sex(){
    return sex;
}



string Bunny::set_color(){
    string color[9] = {"Brown", "Black", "White", "Gray", "Spotted", "Blue",
			"Californian", "Castor", "Cinnamon"};
    return color[randnum(9)];
}

string Bunny::get_color(){
    return color;
}



string Bunny::set_name(){
    //TEST grab name from namelist.txt
    string name[7] = {"Buddy", "Ashes", "Apache", "Ubuntu", "Archie", "Mintie", "Beethoven"};
    return name[randnum(7)];
}

string Bunny::get_name(){
    return name;
}



int Bunny::get_age(){
    return age;
}



bool Bunny::get_RA(){
    return RA;
}

bool Bunny::is_RA(){
    int percent_num = randnum(101);
    if(percent_num <= 2) //give 2% change to spawn a radioactive bunny
        return true;
    else
        return false;

}


Control.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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 


Control.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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
Last edited on
closed account (o3hC5Di1)
Hi there,

Could you post Bunny.h and Bunny.cpp too for us please?

Thanks!

All the best,
NwN
sorry i wasnt sure if to keep the post short or just display all.
I edited the original post and added dependecies
closed account (o3hC5Di1)
Hi there,

include/Bunny.h:4:18: warning: extra tokens at end of #include directive [enabled by default]


In Bunny.h:

#include <string>;

Try removing that semicolon at the end there.

Hope that helps.

All the best,
NwN
oh wow, i didnt even see that. I removed the semicolon, but that apparently wasnt the reason for the original error as i get the same after.

I also found another typo at Control.cpp
#include "../inlcude/Bunny.h"

but that wasnt relevant to the original error either.
Last edited on
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.
Last edited on
closed account (o3hC5Di1)
Hi there,

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.

All the best,
NwN
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).
closed account (o3hC5Di1)
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.

All the best,
NwN
Thanks again.

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.
Last edited on
closed account (o3hC5Di1)
On a sidenote, if you enjoy using vim: http://vimcasts.org/
Really good info on how to get the most out of it :)

All the best,
NwN
Nice!

Thanks
Topic archived. No new replies allowed.