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

Problem with enum and struct

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!

Mar 2 '06 #1
7 2071

Paolo wrote:
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.

--
BR, Vladimir

Mar 2 '06 #2
Paolo wrote:
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;

Mar 2 '06 #3
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?

Mar 2 '06 #4
Paolo wrote:
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'.

Mar 2 '06 #5

"Paolo" <pa****@gmail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
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;
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!

--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Mar 2 '06 #6
"Paolo" <pa****@gmail.com> writes:
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
Mar 2 '06 #7
"Vladimir S. Oka" <no****@btopenworld.com> writes:
Paolo wrote:
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>

--
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.
Mar 2 '06 #8

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

Similar topics

3
by: Rob Jackson | last post by:
HiI've got a struct, known by file A.c, which contains a pointer to struct B. Struct B is unknown by file A.c (it is declared in C.h), and contains a typedef enum, which is declared in a file B.h,...
4
by: Chris | last post by:
I've lurked around long enough... Time to interract =) I'm trying to make sense of the following. I can't quite wrap my head around what this is actually doing: ------------- typedef enum {...
3
by: Jerry | last post by:
Hi, I found an enum type, for example: enum Alignment { Top, Bottom } is derived Enum, but Enum is a struct. However,
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
0
by: Greg | last post by:
Not sure if this is best place for this problem, but here it is. I have a project that is simply a C# class that interfaces with an IFilter. This is so I can retreive the text from Word docs. ...
4
by: PSL | last post by:
Hi, I am going through the training C# For Programmers made by TestOut and like the presentation. The only problem is that this tutorial has a nastie bug that renders this software practically...
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...
1
by: Don.Leri | last post by:
Hi, I have a logger.dll (unmanaged c++ dll compiled in vs2005). I have a C# interop to use that dll in managed code implemented in Interfaces.dll (used by other C# dlls). I also have a...
3
by: desktop | last post by:
When I try to compile this template I get an error in the line: "enum { Yes = sizeof(IsClassT<T>::test<T>(0)) == 1};" saying: blop.hpp:8: error: expected primary-expression before ‘>’ token...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.