Hello all. This is my first time posting, so please let me know if I do something to offend/frustrate you (I know how us new guys can be). Anyways, I'm fairly new to C++ and I came across a problem today that I couldn't figure out. I created a class Bunny that initializes several members based on a random element in a vector. This is what I have so far:
1 2 3 4 5 6 7 8 9
|
class Bunny {
char sex;
std::string color;
int age;
std::string name;
public:
Bunny() : sex(genders[rand() % 2]), color(patterns[rand() % 4]), age(0), name(names[rand() % 7]) {}
};
|
Vectors genders, patterns and names are currently in the global namespace with chars/strings hardcoded in. This code works fine for me, but I don't like having the vectors as global variables. When I wrote my code originally, it looked a little like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
class Bunny {
char sex;
std::string color;
int age;
std::string name;
bool mutant;
std::vector<char> genders = { 'm', 'f' };
std::vector<std::string> patterns = { "white", "brown", "black", "spotted" };
std::vector<std::string> names = { "Fuzzy", "Whitey", "Brownie", "Spotty", "Black Ears", "Cotton Tail", "Herbert Nenninger" };
public:
Bunny() : sex(genders[rand() % 2]), color(patterns[rand() % 4]), age(0), name(names[rand() % 7]) {}
};
|
This threw the following error: 'Bunny::genders': list initialization inside member initializer list or non-static data member initializer is not implemented
(By the way, I'm using Microsoft Visual Studio 2013) From what I could find on the internet, it seems to be an issue with the compiler not having support for C++ 11 yet? Can someone explain what is going on, and how I can fix the program to not have to use the global namespace?
Thanks in advance, and I welcome any criticism. Like I said, I'm pretty new.