Problem passing enum to class

I'm trying to pass an enum to a class, but it's not working. Could someone have a look and tell me what I'm doing wrong? Thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

class Player
{
    public:
        enum Class
        {
            WARRIOR,
            MAGE
        };

        Player(Class object)
        {
            if(object == WARRIOR)
            {
                cout << "Your class is Warrior.\n";
            }
            else if(object == MAGE)
            {
                cout << "Your class is Mage.\n";
            }
        }
};
int main()
{
    Player(MAGE);

    return 0;
}
MAGE is a name at class scope. Qualify it with the name of the class.
1
2
// Player(MAGE);
Player( Player::MAGE ); 
Change line 28:

Player(Player::MAGE);
Okay, now I get this error:
error: invalid use of qualified-name 'Player::MAGE'


Anyone know why?
Last edited on
Can you paste your full code? It compiles on the server(cogwheel in upper right corner of code block).

I'd also argue that "Class" is rather poor name, considering "class" keyword.
Last edited on
That is the full code. I only changed the call to the constructor as advised. My IDE is Code::Blocks, the compiler is GNU GCC and C++11 is enabled, if that helps.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54905

This might be compiler bug. You can either try doing something like Player gamer(Player::MAGE); or Player player1(Player::MAGE), player2(Player::MAGE);but if that doesn't work then I don't know what's wrong.
Last edited on
GCC (or certain versions of it) doesn't like it that you call the constructor without an object. So change to:

Player aplayer(Player::MAGE);
I... honestly don't know why I didn't think to try that. You really know you're not on your game when you don't think to create a bloody object! Anyway, thanks guys, it works now.

Might I ask, do any of you know any good tutorials to do with scope, and specifically class scope? I find this confusing.
Last edited on
Not a tutorial, but this explanation (with annotated code examples) should suffice:
http://en.cppreference.com/w/cpp/language/scope
Thank you.
Topic archived. No new replies allowed.