Enum advice please
-
Hi all,
I have the following code:
typedef enum _DCDBTypes
{
DCOItemType = 0,
DCOCategoryType = 1,
DCORegionType = 2
}DCDBTypes;
It gives me this error message.
error: expected specifier-qualifier-list before 'typedef'
If I try to use it like this
- (void)setItemType:(DCDBTypes) newType;
I get this error message
error: expected ')' before 'DCDBTypes'
I have stared at this piece of code for so long, I know it is
something stupid I have assumed or done, please glance over it and
point me in the right direction if possible.
Regards
Damien
"We act as though comfort and luxury were the chief requirements of
life, when all that we need to make us happy is something to be
enthusiastic about."
-- Albert Einstein -
On 5 Jan 2009, at 6:14 pm, Damien Cooke wrote:>
> Hi all,
> I have the following code:
>
>
> typedef enum _DCDBTypes
> {
> DCOItemType = 0,
> DCOCategoryType = 1,
> DCORegionType = 2
> }DCDBTypes;
>
> It gives me this error message.
>
> error: expected specifier-qualifier-list before 'typedef'
>
> If I try to use it like this
>
> - (void)setItemType:(DCDBTypes) newType;
>
> I get this error message
>
> error: expected ')' before 'DCDBTypes'
>
> I have stared at this piece of code for so long, I know it is
> something stupid I have assumed or done, please glance over it and
> point me in the right direction if possible.
Maybe the error is really on the line above 'typedef'? Compilers are
pretty dumb and keep on parsing until they get *really* confused. The
real error maybe above this.
That said, also try this:
typedef enum
{
DCOItemType = 0,
DCOCategoryType = 1,
DCORegionType = 2
}
DCDBTypes;
I never add in those _blahblah things after enum, I've never
understood what they're for. I always typedef enums like this and they
always work just fine. (Maybe someone could explain what this other
form is all about and whether it matters?)
hth,
Graham -
On Jan 6, 2009, at 1:53 AM, Graham Cox wrote:> On 5 Jan 2009, at 6:14 pm, Damien Cooke wrote:
>>
>> typedef enum _DCDBTypes
>> {
>> DCOItemType = 0,
>> DCOCategoryType = 1,
>> DCORegionType = 2
>>
>> }DCDBTypes;
>
> That said, also try this:
>
> typedef enum
> {
> DCOItemType = 0,
> DCOCategoryType = 1,
> DCORegionType = 2
> }
> DCDBTypes;
>
> I never add in those _blahblah things after enum, I've never
> understood what they're for. I always typedef enums like this and
> they always work just fine. (Maybe someone could explain what this
> other form is all about and whether it matters?)
Start with this pair of declarations. This creates two types,
`DCDBTypes` and `enum _DCDBTypes`:
enum _DCDBTypes {
DCOItemType = 0,
DCOCategoryType = 1,
DCORegionType = 2
};
typedef enum _DCDBTypes DCDBTypes;
C allows a contraction of the above. This creates the same two types:
typedef enum _DCDBTypes {
DCOItemType = 0,
DCOCategoryType = 1,
DCORegionType = 2
} DCDBTypes;
You can omit the separate enum type:
typedef enum {
DCOItemType = 0,
DCOCategoryType = 1,
DCORegionType = 2
} DCDBTypes;
...or even give the two types the "same" name if you want (but not
really; one is `enum DCDBTypes` and the other is just `DCDBTypes`):
typedef enum DCDBTypes {
DCOItemType = 0,
DCOCategoryType = 1,
DCORegionType = 2
} DCDBTypes;
All of this works the same way with struct types.
--
Greg Parker <gparker...> Runtime Wrangler -
On Jan 6, 2009, at 11:19, Greg Parker wrote:> On Jan 6, 2009, at 1:53 AM, Graham Cox wrote:
>> I never add in those _blahblah things after enum, I've never
>> understood what they're for. I always typedef enums like this and
>> they always work just fine. (Maybe someone could explain what this
>> other form is all about and whether it matters?)
>
> Start with this pair of declarations. This creates two types,
> `DCDBTypes` and `enum _DCDBTypes`: ...
I'm sure someone will jump in and correct me if I'm wrong about this,
but (in answer to the implied "why?" in Graham's post) my recollection
is that:
-- 'typedef' was added to C later in its life, so originally 'enum
XXX' was the only way to refer to an enum type you'd already defined.
["Later" in this context would be somewhere in the mid-1970s.] Or
perhaps it was just that some C compilers didn't implement 'typedef'
in olden days.
-- The use of typedefs for enums and structs became popular in C after
C++ became popular, because of the convenient 'enum XXX {...}'/'struct
XXX {...}' declaration form in C++ that was more like a typedef than
the apparently identically C syntax.
But maybe I misremember. -
On 6 Jan 2009, at 19:59, Quincey Morris wrote:>
> I'm sure someone will jump in and correct me if I'm wrong about this,
Jumping in....> but (in answer to the implied "why?" in Graham's post) my
> recollection is that:
>
> -- 'typedef' was added to C later in its life, so originally 'enum
> XXX' was the only way to refer to an enum type you'd already
> defined. ["Later" in this context would be somewhere in the
> mid-1970s.] Or perhaps it was just that some C compilers didn't
> implement 'typedef' in olden days.
enum was added to C after typedef. When I first started learning C in
the mid 80's (from K & R first edition), typedef already existed as a
keyword but enum didn't. In fact, I don't think enum became an
official part of C until the first ANSI standard was published.
However, your whole discussion still makes sense with the word struct
substituted for enum and I'm sure struct predates typedef.> -
On Jan 7, 2009, at 03:12, Jeremy Pereira wrote:> enum was added to C after typedef. When I first started learning C
> in the mid 80's (from K & R first edition), typedef already existed
> as a keyword but enum didn't. In fact, I don't think enum became an
> official part of C until the first ANSI standard was published.
>
> However, your whole discussion still makes sense with the word
> struct substituted for enum and I'm sure struct predates typedef.
Thanks for the correction. That would explain why I couldn't find
'enum' in the index of my copy of K & R. :)


