I guess I don't understand enums?

Ok, so I'm getting some stupid errors. And I'm getting annoyed cause what I thought was straight forward, is now causing my problems.
Bunny.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Bunny {
private:
	enum Sex {Male = 0, Female};
	enum Color{White = 0, Brown, Black, Spotted};
	int age;
	bool radioactive;
	std::string name;

public:
	Bunny();
	Sex getSex();
	void setSex(Sex);
	Color getColor();
	void setColor(Color);
	int getAge();
	void setAge(int);
	std::string getName();
	void setName(std::string);
	bool getRadioactive();

};


Bunny.cpp
1
2
3
4
5
6
7
8
9
10
Bunny::Bunny(){
	srand(time(NULL));
	age = rand() % 11;
	Sex sex  = Sex(rand() & 2);
	Color color = Color(rand() % 4);
}

Bunny::Sex Bunny::getSex(){
	return sex;
}


And the problem is

1
2
3
Bunny::Sex Bunny::getSex(){
	return Sex;
}

It's telling me type name is now allowed for Sex in my return statement.
The syntax for defining an enum is very similar to the syntax for defining a class. You're defining the types but you don't have any objects in your class of that type.

Does that help?

-Albatross
Did I not do that in my constructor?
Yes, and that variable will be lost as soon as your constructor's done. :)

-Albatross
Hmm. So, I need to change something in my header? This whole seperate file thing is killin me
That's right!

Though, you'd have to change something regardless of whether you split the declaration and the implementation or not. :/

-Albatross
Last edited on
On line 8, write
Sex sex;
On line four of the constructor, remove the first 'Sex'

By the way, I recommend using the word "gender" instead ;)
Last edited on
Aha that fixed it! So, just to make sure I understand

enum Sex {Male = 0, Female}; This defines an enum

Sex sex; This creates my enum object

sex = Sex(rand() & 2); This gives it a value

Mmm... that's about right, though I never used the usual constructor notation for enums like that. I'd always use a ternary operator (or if statements) and two or more possible values of the enum object. I find it helps a lot with readability.

@LB
Sex is a physical property. Gender is mental property.

-Albatross
@Albatross, I don't know what you mean there :/
Also, having yet another issue. This is in bunny.cpp, I have name defined in the constructor

1
2
3
std::string getName(){
	return name;
}


Name is undefined, I guess. Not sure why
You forgot the Bunny::. :(

EDIT: @Two posts above this one: As in (rand() % 2)?(Bunny::Male):(Bunny::Female);

-Albatross
Last edited on
Omg, wow. It's always the small things :(

EDIT: Responding to your edit.

That does look much cleaner. Though, I'm not too comfortable with the ternary operator.
Last edited on
I would think that doesn't look cleaner, only more explicit.
Hmm, ok so I just tested my getters/setters. It all works, but it displays the enums as there numeric value. Why is this?
That's because the enums are basically just a way to do away with so-called magic numbers (numbers that are given an unnatural meaning in a program) and #defined constants. The compiler strips away the fancy names at compile-time.

You might want to consider a switch statement for printing out a string associated with each value.

-Albatross
Last edited on
Ah hmm, well thats annoying. I guess I dont need to do that now, I just wrote a quick function to test everything. Values are right, I just dont get words. Thanks though!
Topic archived. No new replies allowed.