473,765 Members | 2,057 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2086

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.c om> wrote in message
news:11******** **************@ u72g2000cwu.goo glegroups.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.c om> 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****@btopenw orld.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_Keit h) 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
7047
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, which is included in file A.c (this is getting really confusing, I know!).I want to access the enum in file A.c, but don't want to include file C.h, for various reasons.I've tried casting the enum, but still get the error "Dereferencing pointer...
4
3055
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 { DOUBLE_LIST, INT_LIST } DATA_TYPE; typedef struct { DATA_TYPE type;
3
2698
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
3940
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. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
0
1718
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. I'm able to use this DLL without any problems within my test windows app, but not within my windows service (that's when I receive the casting exception). Here's the code (sorry it's long): VB Function within windows app and windows service:
4
3512
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 useless. I've tried using TestOut's support form to submit the problem but each time I submit I get a "ERROR MESSAGE: Could not modify CSV file.". My problem is that after I run a simulation, the simualtion report comes up. One section of this...
34
11201
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 snippet that I wrote today that gives me an instant implementation of the pattern. I could easily just always use such an implementation instead of a standard enum, so I wanted to know what you experts all thought. Is there a case for standard enums?
1
4560
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 number of other C# dlls referencing Interfaces.dll and using logger.dll interop for logging.
3
1999
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 #ifndef BLOP_HPP_
0
9568
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10164
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9835
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7379
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3926
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.