473,386 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

enum Assignment

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.
Jul 22 '05 #1
7 4501
Ignaz Krähenmann wrote:
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?

--

Cheers
--
Hewson::Mike
"This letter is longer than usual because I lack the time to make it
shorter" - Blaise Pascal
Jul 22 '05 #2
"Ignaz Krähenmann" <ik**********@bluewin.ch> wrote in message
news:41**********@news.bluewin.ch
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?
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #3
Ignaz Krähenmann wrote:

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;


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4
Ioannis Vranos wrote:
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;


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #5
Igi
I think I got the point.

Thanks to John and Ioannis!
Igi.
Jul 22 '05 #6
John Carson wrote:
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
Jul 22 '05 #7
"Dave Rahardja" <as*@me.com> wrote in message
news:XW********************@twister.rdc-kc.rr.com

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.
--
John Carson
1. To reply to email address, remove donald
2. Don't reply to email address (post here instead)

Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Eric Beyeler | last post by:
I ran into an interesting characteristic of enums... why is comparison between different types allowed? assignment is not allowed ( which I would expect ) enum TESTA { TestA1=4, TestA2=5, };
20
by: Glenn Venzke | last post by:
I'm writing a class with a method that will accept 1 of 3 items listed in an enum. Is it possible to pass the item name without the enum name in your calling statement? EXAMPLE: public enum...
9
by: dumboo | last post by:
hi there, i was looking for some way to give default values to enum, when ever i m creating an enum variable it shuld be INITIALIZED to certain default values is it possible ? or do i have to...
0
by: Vaclav Haisman | last post by:
Motivation: I have been working on some project recently that uses lots of enums with disjunctive intervals of values because it is rather convenient way to define series of constants with...
6
by: Fao | last post by:
Hi, I am in my first year of C++ in college and my professor wants me to Write a Program with multiple functions,to input two sets of user-defined data types: One type named 'Sign' declared by...
13
by: Martin | last post by:
This post asks two questions, which I've illustrated in one C source file (see below), which clean compiles on my GNU compiler. 1. In K&R2, Section A8.9 it says "Declarations whose storage class...
12
by: Laurent Deniau | last post by:
If I understand well, an enumeration is only garantee to hold at most an int (6.7.2.2-2). So I would like to know: how to store a long in an enum? enum { p2_31 = 1L << 31 }; // boom how to...
35
by: dtschoepe | last post by:
Greetings. I am working on an assignment and can't seem to get the right concept for something I'm attempting to do with enum data types. I have defined the following in my code: enum color...
9
by: Jamey Bon | last post by:
As a newbie to C#, I am not sure what I can do about this. I would like to do something like an Enumeration to use "constants" like Yes to indicate true and No for false. But since there seems to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.