1

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.

6
  • 2
    What is return Medal supposed to do? Medal is a type. Commented Mar 14, 2015 at 12:53
  • 2
    Your class doesn't contain any data members. It only contains some member type definitions. Commented Mar 14, 2015 at 12:53
  • Thank you for your comments. return Medal is supposed to return a value of Medal property. I created it because I have some experiences in C# and Java. I wanted to create it like this.Medal as in Java. But, as you mentioned, this way is wrong for C++. Commented Mar 14, 2015 at 13:12
  • I mean return this.Medal... this is probably the easiest way for me to understand. Commented Mar 14, 2015 at 13:13
  • @YMD: Even in Java, you would have to return an instance of the enum. (In Java, you can also return a class, but I'm sure this is absolutely not what you are talking about here.) Commented Mar 14, 2015 at 13:16

3 Answers 3

3
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:

  • You should prefer C++11 enum classes.
  • 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
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you Christian, Daniel and Kerrek. I suppose that I misunderstood the usage of enum, rather than creating classes.
I'd like to ask another simple question. In you sample code (the bottom one which reflects on my understanding on Java), I need to declare a variable for the Medal type in order for getMedal() to work. Is it correct?
@YMD: Yes. For example: return Medal.GOLD;. This returns an object of type Medal - it returns the gold object. Just like it would do in C++, just with a minimally different syntax.
@YMD: Actually, yes and no. return Medal.GOLD; works but you don't declare a variable. With a variable declaration involved, it would be: Medal medal = Medal.GOLD; return medal;
Thank you so much for enlarging my knowledge. I think I've learned a lot :)
2

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) {}
};

Comments

2

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)

3 Comments

Regarding your first sentence, actually it should be: "When you define the member functions, [...]"
@ChristianHackl Fixed. You are absolutely right, thanks.
I just fixed a similar mistake in my own answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.