Topic: enum contents


Author: Danny Kelly <danny.kelly@home.com>
Date: 2000/08/30
Raw View
In an enumeration, can you have values be possible values of other types
(native or user-defined)?

For example, this code gives me an error message when I try to compile
it (without line numbers, of course):

1:  enum CardValue
2:  {
3:      ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king
4:  };


This is the error message the compiler gives me (by the way, I included
an empty main())

Error E2184 enumtest.cpp 3: Enum syntax error
Error E2040 enumtest.cpp 4: Declaration terminated incorrectly
Error E2190 enumtest.cpp 4: Unexpected }
Error E2190 enumtest.cpp 4: Unexpected }

I understand there are several possibilities for this not compiling.

If anyone with a copy of the standard, or ~any~ insight could assist me,
it would be greatly appreciated. This is an issue I have never
encountered in my readings on Standard C++, or at all.

Sam Kelly

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/08/30
Raw View
Danny Kelly wrote:
>
> In an enumeration, can you have values be possible values of other types
> (native or user-defined)?
>
> For example, this code gives me an error message when I try to compile
> it (without line numbers, of course):
>
> 1:  enum CardValue
> 2:  {
> 3:      ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king
> 4:  };

You have to name your enumeration constants. You can't use integer
literals for them. You could use:

 enum CardValue
 {
 ace=1, jack=11, queen, king
 };

This automatically sets 'queen', and 'king' to 12 and 13, respectively.

CardValue would be guaranteed to be able to hold all values from 0 to
15, since that is the range of values that will fit in the smallest
bit-field that can hold all of enumeriation constants. In particular, it
will be able to hold the valid values 2 through 10, as well as the
invalid values 0, 14, and 15.

However, that approach has the disadvantage that '3' is not recognized
as an enumeration constant of type 'enum Cardvalue'. Instead, it's
simply an integer literal that can be converted to and from that type.
That means you can't write things like:

 enum CardValue card=3; //illegal: type mismatch

Instead, you must write:

 enum CardValue card=(enum Cardvalue)3; // legal

The other option is:

 enum CardValue
 {
 ace=1, two, three, four // etc.
 ...

You can then write:

 enum CardValue card=three;

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Victor Bazarov" <vAbazarov@dAnai.com>
Date: Thu, 31 Aug 2000 15:15:38 GMT
Raw View
"Danny Kelly" <danny.kelly@home.com> wrote...
> In an enumeration, can you have values be possible values of other
types
> (native or user-defined)?
>
> For example, this code gives me an error message when I try to compile
> it (without line numbers, of course):
>
> 1:  enum CardValue
> 2:  {
> 3:      ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king
> 4:  };
>
>
> This is the error message the compiler gives me (by the way, I
included
> an empty main())
>
> Error E2184 enumtest.cpp 3: Enum syntax error
> Error E2040 enumtest.cpp 4: Declaration terminated incorrectly
> Error E2190 enumtest.cpp 4: Unexpected }
> Error E2190 enumtest.cpp 4: Unexpected }
>
> I understand there are several possibilities for this not compiling.
>
> If anyone with a copy of the standard, or ~any~ insight could assist
me,
> it would be greatly appreciated. This is an issue I have never
> encountered in my readings on Standard C++, or at all.
>
> Sam Kelly

To tell you the truth, I am not sure what you're asking.

What you probably need to do is

enum CardValue
{
   ace = 1, two, three, four, five, six, seven,
    eight, nine, ten, jack, queen, king
};

The enum constants must be valid identifiers (Standard, 7.2).  And you
can
assign values to them if you want, otherwise each gets a value greater
than
the one on the left by one.

Victor
--
Please remove capital A's from my address when replying by mail



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Balog Pal (mh)" <pasa@lib.hu>
Date: Thu, 31 Aug 2000 16:03:19 GMT
Raw View
Danny Kelly wrote in message <39AD77EC.7FD0BF1@home.com>...

>In an enumeration, can you have values be possible values of other types
>(native or user-defined)?


You can have identifiers, and optionally const initializers to those
identifiers.

>For example, this code gives me an error message when I try to compile
>it (without line numbers, of course):
>
>1:  enum CardValue
>2:  {
>3:      ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king
>4:  };


ace, jack, queen, king is all good, but the numbers can't be used as
identifiers.

>1:  enum CardValue
>2:  {
>3:      c_ace, c_2, c_3, c_4, c_5, c_6, c_7, c_8, c_9, c_10, c_jack,
c_queen, c_king
>4:  };

would definitely compile. You can also attach values, like

c_ace = 11, c_2 = 2, c_3, c_4, c_5, c_6, c_7, c_8, c_9, c_10, c_jack =10,
c_queen =10, c_king =10

Here the identifiers with = take that value, and where none supplied will be
one more then the preceeding (ie c_7 will be 7).

>Error E2184 enumtest.cpp 3: Enum syntax error


Yes.

>Error E2040 enumtest.cpp 4: Declaration terminated incorrectly

That is just sync error, all go away after line 3 is fixed.


Paul


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Jerry Coffin <jcoffin@taeus.com>
Date: Thu, 31 Aug 2000 17:00:30 GMT
Raw View
In article <39AD77EC.7FD0BF1@home.com>, danny.kelly@home.com says...
> In an enumeration, can you have values be possible values of other types
> (native or user-defined)?

I'm not quite sure what you're asking or what you mean.  Maybe you're
asking about the value you can assign to a name?

> For example, this code gives me an error message when I try to compile
> it (without line numbers, of course):
>
> 1:  enum CardValue
> 2:  {
> 3:      ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king
> 4:  };

This looks like complete nonsense to me.  Defining an enum looks
something like this:

enum CardValue {
 ace = 1,  // assign a value.
 deuce,  // value incremented automatically.
 trey,
 three = 3, // assign same value as trey already has.
 four,   // auto increment for remainder of list.
 five,
 six,
 seven,
 eight,
 nine,
 ten,
 jack,
 queen,
 king
};

Note in particular that it makes no sense at all to try to put
numbers (or other constants) in the list -- an enumeration defines
names and gives them values; without the name you don't get anything
meaningful.

Maybe you were trying to ask about the values you assign to the names
and whether they can be things like the values of other objects of
built-in or user-defined types?  If so, the answer is generally no:
the values you use must be constants of integral or enumeration type.
That means you should be able to do something like:

const int start = 1;

enum values { first = start, second, third };

Though it's usually open to question whether this would really
accomplish anything.  Likewise, you should be able to do this:

enum X { a, b, c };
enum Y { d = c, e, f };

though again there may be some question about how useful this usually
is.

--
    Later,
    Jerry.

The Universe is a figment of its own imagination.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Keith Thompson <kst@cts.com>
Date: Thu, 31 Aug 2000 20:02:11 GMT
Raw View
"Balog Pal (mh)" <pasa@lib.hu> writes:
[...]
> You can also attach values, like
>
> c_ace = 11, c_2 = 2, c_3, c_4, c_5, c_6, c_7, c_8, c_9, c_10, c_jack =10,
> c_queen =10, c_king =10
>
> Here the identifiers with = take that value, and where none supplied will be
> one more then the preceeding (ie c_7 will be 7).

Note that this will make it impossible to distinguish among c_10,
c_jack, c_queen, and c_king, since they'll all have the same value.

If you want to associate a value with each enumeration constant, and
the values aren't necessarily unique, declare an array and index it
with the enumeration type.

--
Keith Thompson (The_Other_Keith) kst@cts.com  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Welcome to the last year of the 20th century.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: Fri, 1 Sep 2000 12:34:06 GMT
Raw View
Jerry Coffin wrote:
>
> In article <39AD77EC.7FD0BF1@home.com>, danny.kelly@home.com says...
> > In an enumeration, can you have values be possible values of other types
> > (native or user-defined)?
>
> I'm not quite sure what you're asking or what you mean.  Maybe you're
> asking about the value you can assign to a name?
>
> > For example, this code gives me an error message when I try to compile
> > it (without line numbers, of course):
> >
> > 1:  enum CardValue
> > 2:  {
> > 3:      ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king
> > 4:  };
>
> This looks like complete nonsense to me.  Defining an enum looks
> something like this:
>
> enum CardValue {
>         ace = 1,        // assign a value.
>         deuce,  // value incremented automatically.
>         trey,
>         three = 3,      // assign same value as trey already has.
>         four,           // auto increment for remainder of list.
>         five,
>         six,
>         seven,
>         eight,
>         nine,
>         ten,
>         jack,
>         queen,
>         king
> };
>
> Note in particular that it makes no sense at all to try to put
> numbers (or other constants) in the list -- an enumeration defines
> names and gives them values; without the name you don't get anything
> meaningful.

My guess is that he wanted '3' to be recognized, in appropriate
contexts, as an enumeration constant of type 'enum CardValue'. Don't ask
how that's supposed to work - people often want things that can't be
made to happen.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Danny Kelly <danny.kelly@home.com>
Date: Fri, 1 Sep 2000 17:07:52 GMT
Raw View
Thanks a lot guys!

I'm sorry to bother you with this, but I lost my copy of a Draft of the
Standard. By the way, it's a PDF and you can get info at

www.research.att.com/~bs/C++.html.

Thanks again!
 Sam Kelly


Danny Kelly wrote:
>
> In an enumeration, can you have values be possible values of other types
> (native or user-defined)?
>
> For example, this code gives me an error message when I try to compile
> it (without line numbers, of course):
>
> 1:  enum CardValue
> 2:  {
> 3:      ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, jack, queen, king
> 4:  };
>
> This is the error message the compiler gives me (by the way, I included
> an empty main())
>
> Error E2184 enumtest.cpp 3: Enum syntax error
> Error E2040 enumtest.cpp 4: Declaration terminated incorrectly
> Error E2190 enumtest.cpp 4: Unexpected }
> Error E2190 enumtest.cpp 4: Unexpected }
>
> I understand there are several possibilities for this not compiling.
>
> If anyone with a copy of the standard, or ~any~ insight could assist me,
> it would be greatly appreciated. This is an issue I have never
> encountered in my readings on Standard C++, or at all.
>
> Sam Kelly
>
> ---
> [ comp.std.c++ is moderated.  To submit articles, try just posting with ]
> [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
> [              --- Please see the FAQ before posting. ---               ]
> [ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: Sun, 3 Sep 2000 12:27:34 GMT
Raw View
Danny Kelly wrote:
>
> Thanks a lot guys!
>
> I'm sorry to bother you with this, but I lost my copy of a Draft of the
> Standard. By the way, it's a PDF and you can get info at
>
> www.research.att.com/~bs/C++.html.

Don't bother with the draft; it's out of date. Get the final version,
it's only $18. Follow the links below to find out where:

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]