473,325 Members | 2,480 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,325 software developers and data experts.

enum question

I have the following enum declared:

enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

(it goes on and on like that)

This is what I would like to do:

TOKEN t1 = TOK_ID; // ok
TOKEN t2 = 5; // compile error (cannot convert from
const int to 'enum TOKEN')
TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???

could someone clarify if the 3rd example is ok or not, and what type
of problem I might expect if it isn't ok?

What I am trying to do is represent ASCII values 0-127 as TOKENs (this is
why I
started the TOKEN enum off at '1000' so I had plenty of space at the
start....and I don't
really want to type out 127 values into my enum declaration....can anybody
suggest
an alternate solution?

thanks,
James
Nov 15 '05 #1
10 5734

James Brown wrote:
I have the following enum declared:

enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

(it goes on and on like that)

This is what I would like to do:

TOKEN t1 = TOK_ID; // ok
TOKEN t2 = 5; // compile error (cannot convert from
const int to 'enum TOKEN')
TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???

could someone clarify if the 3rd example is ok or not, and what type
of problem I might expect if it isn't ok?

What I am trying to do is represent ASCII values 0-127 as TOKENs (this is
why I
started the TOKEN enum off at '1000' so I had plenty of space at the
start....and I don't
really want to type out 127 values into my enum declaration....can anybody
suggest
an alternate solution?

thanks,
James


In the expression:

enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

TOK_ID, TOK_NUMBER, and TOK_STRING are constant integer values. In this
case, TOK_ID is = 1000, then TOK_NUMBER is 1001. The reason why the
compiler is b-tching is because you are trying to modify the constant
integer TOK_NUMBER (which has a value of 1001).

Nov 15 '05 #2
"James Brown" <dont_bother> writes:
I have the following enum declared:

enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

(it goes on and on like that)

This is what I would like to do:

TOKEN t1 = TOK_ID; // ok
TOKEN t2 = 5; // compile error (cannot convert from
const int to 'enum TOKEN')
TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???

could someone clarify if the 3rd example is ok or not, and what type
of problem I might expect if it isn't ok?


The declaration "enum TOKEN { ... };" creates a type called
"enum TOKEN". It does not create a type called TOKEN.

Given the type declaration, the declaration
enum TOKEN t2 = 5;
is perfectly legal.

I suspect you're using a C++ compiler. C++ is a different language
with different rules; comp.lang.c++ is down the hall on the left.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #3
"Chad" <cd*****@gmail.com> writes:
James Brown wrote:
I have the following enum declared:

enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

(it goes on and on like that)

This is what I would like to do:

TOKEN t1 = TOK_ID; // ok
TOKEN t2 = 5; // compile error (cannot convert from
const int to 'enum TOKEN')
TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???

could someone clarify if the 3rd example is ok or not, and what type
of problem I might expect if it isn't ok?

What I am trying to do is represent ASCII values 0-127 as TOKENs
(this is why I started the TOKEN enum off at '1000' so I had plenty
of space at the start....and I don't really want to type out 127
values into my enum declaration....can anybody suggest an alternate
solution?
In the expression:

enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };


That's a declaration, not an expression.
TOK_ID, TOK_NUMBER, and TOK_STRING are constant integer values. In this
case, TOK_ID is = 1000, then TOK_NUMBER is 1001.
Yes.
The reason why the
compiler is b-tching is because you are trying to modify the constant
integer TOK_NUMBER (which has a value of 1001).


Look again. There's nothing in the original poster's code that
attempts to modify TOK_NUMBER.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #4

Keith Thompson wrote:
"James Brown" <dont_bother> writes:
I have the following enum declared:

enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

(it goes on and on like that)

This is what I would like to do:

TOKEN t1 = TOK_ID; // ok
TOKEN t2 = 5; // compile error (cannot convert from
const int to 'enum TOKEN')
TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???

could someone clarify if the 3rd example is ok or not, and what type
of problem I might expect if it isn't ok?


The declaration "enum TOKEN { ... };" creates a type called
"enum TOKEN". It does not create a type called TOKEN.

Given the type declaration, the declaration
enum TOKEN t2 = 5;
is perfectly legal.

I suspect you're using a C++ compiler. C++ is a different language
with different rules; comp.lang.c++ is down the hall on the left.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


I'm calling an offsides on this one. Maybe I mis-understood the
question. I always thought when you has a construction like

enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

Then TOK_NUMBER = 1001 and TOK_STRING = 1002. Both of these being
constant integers (Page 39 of the "C Programming Language" by K & R).
Hence these values could not be modified because they are constant.

Nov 15 '05 #5
Chad wrote:
Keith Thompson wrote:
"James Brown" <dont_bother> writes:
I have the following enum declared:

enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

(it goes on and on like that)

This is what I would like to do:

TOKEN t1 = TOK_ID; // ok
TOKEN t2 = 5; // compile error (cannot convert from
const int to 'enum TOKEN')
TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???

could someone clarify if the 3rd example is ok or not, and what type
of problem I might expect if it isn't ok?


The declaration "enum TOKEN { ... };" creates a type called
"enum TOKEN". It does not create a type called TOKEN.

Given the type declaration, the declaration
enum TOKEN t2 = 5;
is perfectly legal.

I suspect you're using a C++ compiler. C++ is a different language
with different rules; comp.lang.c++ is down the hall on the left.


I'm calling an offsides on this one. Maybe I mis-understood the
question. I always thought when you has a construction like

enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

Then TOK_NUMBER = 1001 and TOK_STRING = 1002. Both of these being
constant integers (Page 39 of the "C Programming Language" by K & R).
Hence these values could not be modified because they are constant.


I am not sure what you mean.
The
enum TOKEN {
....
};
declaration gives us the type "enum TOKEN", quite like
struct foo {
....
};
gives us the type "struct foo".

So,
enum TOKEN t2;
t2 = 5;
or
enum TOKEN t2 = 5;
are structurally not different from
struct foo bar;
bar = baz;
or
struct foo bar = baz;
where baz is of type struct foo.

Declaring t2 certainly does not change any of the enumeration
constants, neither does initializing t2.
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 15 '05 #6

"Keith Thompson" <ks***@mib.org> wrote in message
news:ln************@nuthaus.mib.org...
"James Brown" <dont_bother> writes:
I have the following enum declared:

enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

(it goes on and on like that)

This is what I would like to do:

TOKEN t1 = TOK_ID; // ok
TOKEN t2 = 5; // compile error (cannot convert from
const int to 'enum TOKEN')
TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???

could someone clarify if the 3rd example is ok or not, and what type
of problem I might expect if it isn't ok?


The declaration "enum TOKEN { ... };" creates a type called
"enum TOKEN". It does not create a type called TOKEN.

Given the type declaration, the declaration
enum TOKEN t2 = 5;
is perfectly legal.

I suspect you're using a C++ compiler. C++ is a different language
with different rules; comp.lang.c++ is down the hall on the left.

--
Keith Thompson (The_Other_Keith) ks***@mib.org
<http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*>
<http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


Hi,
thanks for your answer - and you're right, I am using C++ but its useful for
me
to appreciate the differences....I'll repost on c.l.c++

thanks,
James
Nov 15 '05 #7
"Chad" <cd*****@gmail.com> writes:
Keith Thompson wrote:
"James Brown" <dont_bother> writes:
> I have the following enum declared:
>
> enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
>
> (it goes on and on like that)
>
> This is what I would like to do:
>
> TOKEN t1 = TOK_ID; // ok
> TOKEN t2 = 5; // compile error (cannot convert from
> const int to 'enum TOKEN')
> TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???
>
> could someone clarify if the 3rd example is ok or not, and what type
> of problem I might expect if it isn't ok?
The declaration "enum TOKEN { ... };" creates a type called
"enum TOKEN". It does not create a type called TOKEN.

Given the type declaration, the declaration
enum TOKEN t2 = 5;
is perfectly legal.

I suspect you're using a C++ compiler. C++ is a different language
with different rules; comp.lang.c++ is down the hall on the left.

[...] I'm calling an offsides on this one. Maybe I mis-understood the
question. I always thought when you has a construction like

enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

Then TOK_NUMBER = 1001 and TOK_STRING = 1002. Both of these being
constant integers (Page 39 of the "C Programming Language" by K & R).
Hence these values could not be modified because they are constant.


Of course you can't modify TOK_NUMBER or TOK_STRING.

The posted code (see above) doesn't attempt to do so, and I don't see
anything that would lead you to believe that it does.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 15 '05 #8
James Brown a écrit :
enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

TOKEN t1 = TOK_ID; // ok
TOKEN t2 = 5; // compile error (cannot convert from
const int to 'enum TOKEN')
Be sure you are using a C compiler. The C-language is not that strongly
typed. This line is fine C.
TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???


It's fine C too.

--
C is a sharp tool
Nov 15 '05 #9
Emmanuel Delahaye wrote:
James Brown a écrit :
enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

TOKEN t1 = TOK_ID; // ok
TOKEN t2 = 5; // compile error (cannot convert
from const int to 'enum TOKEN')

Be sure you are using a C compiler. The C-language is not that strongly
typed. This line is fine C.
TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???

It's fine C too.


Ah. This is obviously some strange usage of the word
"fine" that I wasn't previously aware of.

enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };
TOKEN t1 = TOK_ID;
TOKEN t2 = 5;
TOKEN t3 = (TOKEN)5;

gcc -W -Wall -ansi -pedantic -c token.c
token.c:2: error: parse error before "t1"
token.c:2: warning: type defaults to `int' in declaration of `t1'
token.c:2: error: ISO C forbids data definition with no type or storage
class
token.c:3: error: parse error before "t2"
token.c:3: warning: type defaults to `int' in declaration of `t2'
token.c:3: error: ISO C forbids data definition with no type or storage
class
token.c:4: error: parse error before "t3"
token.c:4: warning: type defaults to `int' in declaration of `t3'
token.c:4: error: `TOKEN' undeclared here (not in a function)
token.c:4: error: parse error before numeric constant

--
Eric Sosman
es*****@acm-dot-org.invalid
Nov 15 '05 #10
Eric Sosman a écrit :
enum TOKEN { TOK_ID = 1000, TOK_NUMBER, TOK_STRING };

TOKEN t3 = (TOKEN)5; // compiles but I think it's illegal???


It's fine C too.


Ah. This is obviously some strange usage of the word
"fine" that I wasn't previously aware of.


Ah sh*t, I missed the 'enum' word... Sorry about that.

--
C is a sharp tool
Nov 15 '05 #11

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

Similar topics

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...
4
by: Nikhil Patel | last post by:
Hi all, I am a VB6 programmer and learning C#. I am currently reading a chapter on types. I have question regarding enums. Why do we need to convert enum members to the value that they represent?...
5
by: Andrea Williams | last post by:
I'm working with C# and I'm setting up some ENUM's I have a data and Business layer. I'm declaring a common enum for the Data Layer. The UI layer references the Bus layer and the bus layer...
6
by: Michael Isaacs | last post by:
Regarding use of enum's, I am wondering what the cost of memory is when creating the enumeration on the calling side, and then using it on the function/method side. See example below. If I...
2
by: Dennis | last post by:
I have an enum as follows: Public Enum myData FirstData = 6 SecondData = 7 end enum Is there anyway that I can return the Enum names by their value, i.e., I want to input 6 into a function...
13
by: Don | last post by:
How do I get an Enum's type using only the Enum name? e.g. Dim enumType as System.Type Dim enumName as String = "MyEnum" enumType = ???(enumName)
3
by: K. Wilder | last post by:
I need to declare a project level Enum that any procedure in any class can reference so there's continuity with the values. How do I do this? At present, if I declare a Module in a project and...
34
by: Steven Nagy | last post by:
So I was needing some extra power from my enums and implemented the typesafe enum pattern. And it got me to thinking... why should I EVER use standard enums? There's now a nice little code...
4
by: ice8595 | last post by:
Hi there, I'm fairly new at this, and I am having a bit of trouble wrapping my head around some concepts of enum for a project. So any help would be greatly appreciated. Essentially I'm...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.