Reputation: 4163
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.
Upvotes: 1
Views: 178
Reputation: 27528
return Medal;
Medal
is the name of the enum
itself. You cannot return the enum type itself. You must return a specific enum object.
The misunderstanding probably starts at your class definition:
class Athletics
{
// ...
enum Medal {GOLD, SILVER, BRONZE};
};
You seem to think that this creates both a type and an object of that type. This is wrong. It just declares a type. You need to give your class an object. And you must make Medal
public if outside code should use it.
Another error:
Athletics::Medal getMedal() { // ... }
This is not a definition of the member function Athletics::getMedal
but of a new, free-standing function getMedal
.
It should be:
Athletics::Medal Athletics::getMedal() {
// ...
}
Finally, an identifier like 100_METER_RACE
should result in a compilation error.
Here is a fixed version of your code:
// athletics.h:
#ifndef ATHLETICS_H
#define ATHLETICS_H
class Athletics
{
public:
enum Medal {GOLD, SILVER, BRONZE};
enum Event {ONE_HUNDRED_METER_RACE, POLE_VAULT, HAMMER_THROW};
Athletics();
Athletics(Medal medal, Event event);
Medal getMedal();
private:
Medal medal;
Event event;
};
#endif
// athletics.cpp:
Athletics::Athletics()
{
}
Athletics::Athletics(Medal medal, Event event) :
medal(medal),
event(event)
{
}
Athletics::Medal Athletics::getMedal() {
return medal;
}
// main.cpp:
int main()
{
Athletics athletics(Athletics::GOLD, Athletics::ONE_HUNDRED_METER_RACE);
}
There are still many things to be fixed, mind you. For example, what should the default constructor do with the two member variables?
Two more observations:
ALL_CAPS
should only be used for preprocessor macros.Edit:
You mention Java in one of your comments, and that such code as originally posted by you is supposed to work there. I don't know what you mean, because your C++ code would be roughly equivalent to the following in Java, and produce an error as well:
class Athletics
{
private static enum Medal { GOLD, SILVER, BRONZE }
public Medal getMedal() {
return this.Medal; // error
}
}
Upvotes: 3
Reputation: 56863
When you define the member function, you need to tell the compiler it's a method of Athletics
:
Athletics::Medal Athletics::getMedal() { ... }
Also, you should not declare another enum
in the implementation file, reuse the one from Athletics
. This also means changing the definition of the constructor:
Athletics::Athletics(Athletics::Medal medal, Athletics::Event event)
Upvotes: 2
Reputation: 477040
I'm not entirely sure where your confusion lies, so maybe a corrected version of the code is helpful. First off, you have to realize that enums are types, just like int
and std::string
. Defining a type does not define any objects; you still have to define your own objects.
struct Athletics
{
enum Medal { "foo", "bar" }; // defines a type Athletics::Medal
Medal m_; // declares a data member
Medal getMedal() const { return m_; }
Athletics(Medal m) : m_(m) {}
};
Upvotes: 2