Why will my overload constructor not let me put values into my arrays?

I'm still pretty new to classes so what am i doing in this code that is wrong.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>

using namespace std;

class BunnyInfo{
    string NewSex[2];
    string NewColor[4];
    int NewAge[11];
    string NewName[20];
public:
    BunnyInfo();
    BunnyInfo(string, string, int, string);
};

BunnyInfo::BunnyInfo(string sex[2], string color[4], int age[11], string name[20]){
    sex[2] = {"Male", "Female"};
    color[4] = {"White", "Brown", "Black", "Spotted"};
    age[11] = {0,1,2,3,4,5,6,7,8,9,10};
    
}


> I'm still pretty new to classes
But I suppose that you have worked with functions and arrays before.
Then you should realize that when you do sex[2] = {"Male", "Female"}; you are accessing the third element of the array that you passed as a parameter.

Also you'll note that the prototypes in the declaration and the definition do not match.


To initialize members, use the initialization list
1
2
3
4
5
6
7
8
9
class foo{
   int bar;
   double d[42];
   foo(): //note colon
      bar(13),
      d({3.14, 2.79}) //c++11
   { //here all the members were already constructed
   }
};



However, ¿do you really want for every BunnyInfo object to hold all those members?
Maybe it would make more sense if they were class members
1
2
3
4
5
6
class BunnyInfo{
   static std::string Sex[2];
   //...
};

std::string BunnyInfo::Sex[2] = {"Male", "Female"};
Last edited on
Okay but how would i get it to print out the value in main.
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
#include <iostream>
#include <stdlib.h>
#include <time.h>
#include <string>

using namespace std;

class BunnyInfo{
public:
    static string Sex[];
    static string Color[];
    static int Age[];
    static string Name[];
    BunnyInfo();
};

string BunnyInfo::Sex[] = {"Male", "Female"};
string BunnyInfo::Color[] = {"White", "Brown", "Black", "Spotted"};
int BunnyInfo::Age[] = {0,1,2,3,4,5,6,7,8,9,10};
string BunnyInfo::Name[] = {"Ness", "Lope", "Fad", "Butt", "Nutt", "Dell","Fell", "Hag", "Chan","Gal"};


int main(){
    BunnyInfo bunny;
    cout<<bunny.Name[1];
}


When i run this it says build failed but without showing where the failure occurred.
change your compiler build suite immediately (or learn to use it)

/tmp/ccheUJmZ.o: In function `main':
foo.cpp:24: undefined reference to `BunnyInfo::BunnyInfo()'
collect2: error: ld returned 1 exit status

http://www.cplusplus.com/forum/general/113904/


By the way, you do not need an object to access class members
std::cout << BunnyInfo::Name[1] << '\n';
Last edited on
How would I change the build suite. Where is it located. I couldn't find anything in the link you mentioned to fix it, if there was I couldn't understand it at all.
> How would I change the build suite. Where is it located.

You do not have to change your build suite; it is just fine as it is.

If you look at the messages output during the build, you would see something like:
'undefined reference to `BunnyInfo::BunnyInfo'
You have declared a constructor for BunnyInfo on line 14, but you have not defined it.

The fix is easy:
You could define it: BunnyInfo() {} // line 14
You could let it be implicit: BunnyInfo() = default ; // line 14
Or you could just comment out or delete that line: // BunnyInfo() ; // line 14

Any of these would work.

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
#include <iostream>

// #include <stdlib.h>
#include <cstdlib> // if needed

// #include <time.h>
#include <ctime> // if needed

#include <string>

using namespace std;

class BunnyInfo{
public:
    static string Sex[];
    static string Color[];
    static int Age[];
    static string Name[];
    // BunnyInfo(); //
};

string BunnyInfo::Sex[] = {"Male", "Female"};
string BunnyInfo::Color[] = {"White", "Brown", "Black", "Spotted"};
int BunnyInfo::Age[] = {0,1,2,3,4,5,6,7,8,9,10};
string BunnyInfo::Name[] = {"Ness", "Lope", "Fad", "Butt", "Nutt", "Dell","Fell", "Hag", "Chan","Gal"};


int main(){
    BunnyInfo bunny;
    cout<<bunny.Name[1];
}

http://coliru.stacked-crooked.com/a/c3bf8b795bb53919
Last edited on
Thanks that solved it.
Topic archived. No new replies allowed.