Enums

Hello. I'm having a problem using enum objects as non-type parameters in class templates. Consider the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
enum Team
{

 Chelsea,
 Real Madrid

};

template<class T>
class Player<Team t>
{
 ...
 ...
 class definition

};




This code compiles without giving any sort of errors, but when I try to instantiate the class like this,

1
2
3

Player<Team::Chelsea>* player;


it gives me errors:
'Chelsea' : illegal qualified name in member declaration
'player' : undeclared identifier
syntax error : missing ')' before ';'
syntax error : missing '>' before ';'


whereas when I do this

1
2
3

Player<(Team)1>* player;


, it compiles successfully. i'm confused as to why this happens. Any suggestions would be helpful

Thanks
Rahul
I'm not sure if it is causing the error you see, but it will not be happy with the space between Real and Madrid, try using an underscore
Enums are not objects, nor are they even complete types.
You need:

1
2
3
4
template< Team T >
class Player {
    // ...
};


and then you instantiate like this:

 
Player<Chelsea>* player;


(The values defined in an enum are not scoped within the enum.)
I'm sorry. I had used an underscore previously. I did not type it here. And yes, I even tried qualifying the enum values within the enum scope, like this:

1
2
3
4

Player<Team::Chelsea>* player;



It gives the same errors.
Please help
From my experience, the scope-resolution operator (::) isn't used with enumerations.
Just try:
Player<Chelsea>* player;, instead of
Player<Team::Chelsea>*player;.
Last edited on
You cannot use scope-resolution on enumerated types. Only namespaces or classes.
Topic archived. No new replies allowed.