Problem with enum and struct

P

Paolo

Hi all!
As I said in the title, I have a very strange problem compiling a
program Here's the code that gives the problem:

enum letters
{
a = 0,
b,
c
};

typedef struct
{
letters let;
int number;
} someStruct;

It gives me an error before letters in the struct. Any idea on
what generates this error? I have to say that this was a C++ code, but
it doesn't use any class or anything else incompatible with C, at least
I think so.
Thanks for the help!
 
V

Vladimir S. Oka

Paolo said:
Hi all!
As I said in the title, I have a very strange problem compiling a
program Here's the code that gives the problem:

enum letters
{
a = 0,
b,
c
};

typedef struct
{
letters let;
int number;
} someStruct;

It gives me an error before letters in the struct. Any idea on
what generates this error? I have to say that this was a C++ code, but
it doesn't use any class or anything else incompatible with C, at least
I think so.

I don't know about C++ (ask in comp.lang.c++), but in C an `enum` does
not define a type, so you'd need:

enum letters let;

in your structure.
 
C

Carl R. Davies

Paolo said:
enum letters
{
a = 0,
b,
c
};

typedef struct
{
letters let;
int number;
} someStruct;

typedef struct
{
enum letters let;
int number;
} someStruct;

or you could declare another typedef:

typedef enum letters letters_e;

then:

typedef struct
{
letters_e let;
int number;
} someStruct;
 
F

Fred Kleinschmidt

Paolo said:
Hi all!
As I said in the title, I have a very strange problem compiling a
program Here's the code that gives the problem:

enum letters
{
a = 0,
b,
c
};

If you want to use "letters" as a type name, as you do in someStruct below,
you have to typedef it.
typedef enum {
a=0,
b,
c
} letters;
 
P

Paolo

Thank you, I think I resolved. Another question, if I have a method
that returns an enum value, for example

letters GetFirstLetter(char* words);

do I have to add enum before letters?
 
C

Carl R. Davies

Paolo said:
Thank you, I think I resolved. Another question, if I have a method
that returns an enum value, for example

letters GetFirstLetter(char* words);

do I have to add enum before letters?

IMHO yes. The word 'letters' alone doesn't denote a type, it must be
'enum letters' for it to be recognised as such. I always prefer
typedef(ing) enums and appending '_e', like 'letters_e'.
 
M

Micah Cowan

Paolo said:
Thank you, I think I resolved. Another question, if I have a method
that returns an enum value, for example

letters GetFirstLetter(char* words);

do I have to add enum before letters?

Yes, in C.

In your orinal post, you claimed that the code was C++ (which is
off-topic here, since /this/ group discusses only C). If that were
true, your code should have compiled (as would the above). It seems
very likely that you were compiling your "C++" code as C code, which
is why our solution worked. If it's really C++ code, make sure you're
compiling it with a C++ compiler. For implementations playing multiple
roles, this probably means you need to make sure your file ends in
..cc, .cx, or .C, rather than .c.

A lot of what I've just said is not really related to what we discuss
here on this newsgroup. If you would like your code to be C++ code,
and want to ask further questions about C++ code, ask
comp.lang.c++. If you want to ask how to get your compiler to compile
the code as C++ code, ask on a group related to your compiler.

HTH
Micah
 
K

Keith Thompson

Vladimir S. Oka said:
Paolo said:
Hi all!
As I said in the title, I have a very strange problem compiling a
program Here's the code that gives the problem:

enum letters
{
a = 0,
b,
c
};
[...]

I don't know about C++ (ask in comp.lang.c++), but in C an `enum` does
not define a type, so you'd need:

enum letters let;

in your structure.

Yes, an enum does define a type. The name of the type is
"enum letters", not "letters".

<OT>In C++, the type can be referred to as just "letters"; likewise
for classes, unions, and structs. This is one of the differences
between C and C++.</OT>
 

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

Forum statistics

Threads
474,175
Messages
2,570,947
Members
47,498
Latest member
yelene6679

Latest Threads

Top