enum Assignment

  • Thread starter =?ISO-8859-1?Q?Ignaz_Kr=E4henmann?=
  • Start date
?

=?ISO-8859-1?Q?Ignaz_Kr=E4henmann?=

Hello
I'm back to C++ programming after many years and forgot quite a lot. Can
someone help me with the following enum issue:

enum type1 {OK = 0, NOK};
enum type2 {OK = 100, FAIL};

type1 oknok;
type2 okfail;

oknok = OK;
okfail = OK;

It seems to me that it is quite obvious what the compiler needs to do:
If the variable is of type1 it should use 0 for OK and for type2 OK
should be 100. Or, more general, the variable type defines the possible
values through the declaration.
gcc, however, issues an error saying "conflicting types for 'OK'". Why
should this not be possible?

Intrestingly, if I code the following:

oknok = FAIL;

gcc sais: "cannot convert 'type2' to 'type1' in assignment". So
obviously the compiler knows which value is possible for which variable.

What am I getting wrong? Thanks.
 
M

Mike Hewson

Ignaz said:
Hello
I'm back to C++ programming after many years and forgot quite a lot. Can
someone help me with the following enum issue:

enum type1 {OK = 0, NOK};
enum type2 {OK = 100, FAIL};

type1 oknok;
type2 okfail;

oknok = OK;
okfail = OK;

It seems to me that it is quite obvious what the compiler needs to do:
If the variable is of type1 it should use 0 for OK and for type2 OK
should be 100. Or, more general, the variable type defines the possible
values through the declaration.
gcc, however, issues an error saying "conflicting types for 'OK'". Why
should this not be possible?

Intrestingly, if I code the following:

oknok = FAIL;

gcc sais: "cannot convert 'type2' to 'type1' in assignment". So
obviously the compiler knows which value is possible for which variable.

What am I getting wrong? Thanks.

So, 'OK' is an identifier with two different values?
Which one is used to initialise which variable?
 
J

John Carson

Ignaz Krähenmann said:
Hello
I'm back to C++ programming after many years and forgot quite a lot.
Can someone help me with the following enum issue:

enum type1 {OK = 0, NOK};
enum type2 {OK = 100, FAIL};

type1 oknok;
type2 okfail;

oknok = OK;
okfail = OK;

It seems to me that it is quite obvious what the compiler needs to do:
If the variable is of type1 it should use 0 for OK and for type2 OK
should be 100. Or, more general, the variable type defines the
possible values through the declaration.
gcc, however, issues an error saying "conflicting types for 'OK'". Why
should this not be possible?

Intrestingly, if I code the following:

oknok = FAIL;

gcc sais: "cannot convert 'type2' to 'type1' in assignment". So
obviously the compiler knows which value is possible for which
variable.
What am I getting wrong? Thanks.


It is just a limitation of enums I am afraid. The compiler does know the
range of values permissible for each type, but does not store separate lists
of identifiers for each type. There is only one OK, not two.

Note that you could assign OK to an int variable. If there were two OK
identifiers, which one should be used?
 
I

Ioannis Vranos

Ignaz said:
Hello
I'm back to C++ programming after many years and forgot quite a lot. Can
someone help me with the following enum issue:

enum type1 {OK = 0, NOK};
enum type2 {OK = 100, FAIL};

type1 oknok;
type2 okfail;

oknok = OK;
okfail = OK;

It seems to me that it is quite obvious what the compiler needs to do:
If the variable is of type1 it should use 0 for OK and for type2 OK
should be 100. Or, more general, the variable type defines the possible
values through the declaration.
gcc, however, issues an error saying "conflicting types for 'OK'". Why
should this not be possible?

Intrestingly, if I code the following:

oknok = FAIL;

gcc sais: "cannot convert 'type2' to 'type1' in assignment". So
obviously the compiler knows which value is possible for which variable.

What am I getting wrong? Thanks.



There are two identifies with the same name and thus the error. See it
this way:


const int x=1;

const float x=2;
 
I

Ioannis Vranos

Ioannis said:
There are two identifies with the same name and thus the error. See it
this way:


const int x=1;

const float x=2;


Perhaps more comprehensible:


const int x=1;

const float x=2;

int y=x;
 
D

Dave Rahardja

John said:
It is just a limitation of enums I am afraid. The compiler does know the
range of values permissible for each type, but does not store separate
lists of identifiers for each type. There is only one OK, not two.

Note that you could assign OK to an int variable. If there were two OK
identifiers, which one should be used?

If there was one thing I could change about C++, this would probably be
it. Enums are weakly supported in the language, but they're handy to
have around.

I wish it were like this...

enum type_1 { OK = 3, CANCEL, PANIC };
enum type_2 { OK = 4, WHAT, DOH };

type_1 a = OK; // a <- 3
type_2 b = OK; // b <- 4
type 1 c = WHAT; // Error
int d = OK; // Error; ambiguous
int e = type_1::OK; // e <- 3
int f = type_2::OK; // f <- 4

-dr
 
J

John Carson

Dave Rahardja said:
If there was one thing I could change about C++, this would probably
be it. Enums are weakly supported in the language, but they're handy
to have around.

I wish it were like this...

enum type_1 { OK = 3, CANCEL, PANIC };
enum type_2 { OK = 4, WHAT, DOH };

type_1 a = OK; // a <- 3
type_2 b = OK; // b <- 4
type 1 c = WHAT; // Error
int d = OK; // Error; ambiguous
int e = type_1::OK; // e <- 3
int f = type_2::OK; // f <- 4

-dr

Looks good to me. For convenience and backward compatibility, I would also
want:

int g = WHAT; // unambiguous so allowed.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top