I'm teaching myself C++. I've been trying to create a class with enum variables, but it produces errors.
Here are some part of my coding.
*A class which will be used for Main()
#include "Athletics.h"
enum Medal {GOLD, SILVER, BRONZE};
enum Event {100_METER_RACE, POLE_VAULT, HAMMER_THROW};
Athletics::Athletics()
{
}
Athletics::Athletics(Medal medal, Event event)
{
Medal m = medal;
Event e = event;
}
Athletics::Medal getMedal(){// produces an error, "within this context"
return Medal; //produces an error, "expected primary-expression before ';' token"
}
*The header of the class above
#ifndef ATHLETICS_H
#define ATHLETICS_H
class Athletics
{
private:
enum Medal {GOLD, SILVER, BRONZE}; //produces an error, "'enum Athletics::Medal' is private."
enum Event {100_METER_RACE, POLE_VAULT, HAMMER_THROW};
public:
Athletics();
Athletics(Medal medal, Event event);
Medal getMedal();
};
#endif
When I reject all those enums and associated constructors, everything seems fine.
Along with a getter ( Medal getMedal()), I'd like to create setter methods for the enum variables.
Unfortunately my text book apparently doesn't have information about why these problems happen.
I'd appreciate if you'd give any advice.
return Medalsupposed to do?Medalis a type.return Medalis supposed to return a value ofMedalproperty. I created it because I have some experiences in C# and Java. I wanted to create it likethis.Medalas in Java. But, as you mentioned, this way is wrong for C++.return this.Medal... this is probably the easiest way for me to understand.