473,473 Members | 1,460 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Enumeration in C and C++

Friends,
I was trying to port a system program implemented in C to C++. I am
having hard time converting enumerations defined in C to C++ and
making them work.

For example if I take the following code fragement,
assuming this is test.c

/* PROGRAM STARTS HERE...*/
#include <stdio.h>

struct test{
enum
{RED, GREEN, BLUE
}color;
};

void amIColored(struct test * t)
{
if (t->color==RED)
printf("\n RED");
else
printf("\n I DO NOT KNOW MY COLOR");

}
int main()
{
struct test t;
t.color= RED;
amIColored(&t);
return 0;
}
/* PROGRAM ENDS HERE */

The above program will work fine if I say
gcc -c test.c

but when I rename the test.c as test.cpp and recompile it using
g++ -c test.cpp
it gives an error related to enumeration. Please let me know where
the mistake is.

PS: I still want to keep structure in the above program as it is
without converting into a class.
Jul 22 '05 #1
5 1714
"Saikrishna" <ve******@yahoo.com> wrote...
Friends,
I was trying to port a system program implemented in C to C++. I am
having hard time converting enumerations defined in C to C++ and
making them work.

For example if I take the following code fragement,
assuming this is test.c

/* PROGRAM STARTS HERE...*/
#include <stdio.h>

struct test{
enum
{RED, GREEN, BLUE
}color;
};

void amIColored(struct test * t)
{
if (t->color==RED)
All enumerators declared in struct 'test' are local to that struct.
In order to access those, you need to help the compiler to find it:

if (t->color == test::RED)
printf("\n RED");
else
printf("\n I DO NOT KNOW MY COLOR");

}
int main()
{
struct test t;
t.color= RED;
Here too

t.color = test::RED;
amIColored(&t);
return 0;
}
/* PROGRAM ENDS HERE */

The above program will work fine if I say
gcc -c test.c

but when I rename the test.c as test.cpp and recompile it using
g++ -c test.cpp
it gives an error related to enumeration. Please let me know where
the mistake is.

PS: I still want to keep structure in the above program as it is
without converting into a class.


There is no difference between struct and class in this case. Your
'test' _is_ a class. It's just called 'struct'.

Victor
Jul 22 '05 #2
Victor Bazarov replied to Saikrishna:
I was trying to port a system program implemented in C to C++. I am
having hard time converting enumerations defined in C to C++ and
making them work.

For example if I take the following code fragement,
assuming this is test.c

/* PROGRAM STARTS HERE...*/
#include <stdio.h>

struct test{
enum
{RED, GREEN, BLUE
}color;
};

void amIColored(struct test * t)
{
if (t->color==RED)


All enumerators declared in struct 'test' are local to that struct.
In order to access those, you need to help the compiler to find it:

if (t->color == test::RED)


[snip]

Or just move the enum outside the struct, if you want to avoid using the
scope resolution operator "::"

enum colorType { RED, GREEN, BLUE };
struct test {
colorType color;
};

David Fisher
Sydney, Australia
Jul 22 '05 #3
"David Fisher" <da***@hsa.com.au> wrote in message
news:H_*******************@nasal.pacific.net.au...
Victor Bazarov replied to Saikrishna:
"Saikrishna" <ve******@yahoo.com> wrote in message
struct test{
enum
{RED, GREEN, BLUE
}color;
};
All enumerators declared in struct 'test' are local to that struct.
In order to access those, you need to help the compiler to find it:
enum colorType { RED, GREEN, BLUE };
struct test {
colorType color;
};


A good solution, though when moving the enum to the outside, I normally
rename it. In this case, something like TestColorType. (Although the
colorType seems general enough so we could keep it named that way.)

But note that the original code had an anonymous enum and Victor's solution
respects this, whereas David's design uses a named enum. It's not a problem
though.

Another thing I've done that works sometimes is to make a class that just
defines enums. Then derive A and B from this class of enums. This way
member functions of A and B don't have to use the qualifier.
Jul 22 '05 #4

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:vHcVc.41240$mD.18203@attbi_s02...
"Saikrishna" <ve******@yahoo.com> wrote...
Friends,
I was trying to port a system program implemented in C to C++. I am
having hard time converting enumerations defined in C to C++ and
making them work.

For example if I take the following code fragement,
assuming this is test.c

/* PROGRAM STARTS HERE...*/
#include <stdio.h>

struct test{
enum
{RED, GREEN, BLUE
}color;
};

void amIColored(struct test * t)
{
if (t->color==RED)


All enumerators declared in struct 'test' are local to that struct.
In order to access those, you need to help the compiler to find it:

if (t->color == test::RED)
printf("\n RED");
else
printf("\n I DO NOT KNOW MY COLOR");

}
int main()
{
struct test t;
t.color= RED;


Here too

t.color = test::RED;
amIColored(&t);
return 0;
}
/* PROGRAM ENDS HERE */

The above program will work fine if I say
gcc -c test.c

but when I rename the test.c as test.cpp and recompile it using
g++ -c test.cpp
it gives an error related to enumeration. Please let me know where
the mistake is.

PS: I still want to keep structure in the above program as it is
without converting into a class.


There is no difference between struct and class in this case. Your
'test' _is_ a class. It's just called 'struct'.

Victor


Can someone explain why this would even compile in C? How would it know
where to find 'RED'?
Jul 22 '05 #5
Method Man wrote:
[...]
Can someone explain why this would even compile in C? How would it know
where to find 'RED'?


Questions on C language should generally be asked in comp.lang.c.
My guess is that since C doesn't have a "class scope" concept, the
enumerators are inserted into the surrounding the struct scope (the
global scope in your case). That makes them available to the entire
translation unit.

Check with more knowledgeable C people.

Victor
Jul 22 '05 #6

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

Similar topics

1
by: Justin Wright | last post by:
I know that I can set up an enumeration as follows ( just typed in quick so may have syntax errors ): <xsd:simpleType name="colors"> <xsd:restriction base="xsd:string"> <xsd:enumeration...
8
by: aevans1108 | last post by:
Greetings I can't seem to inherit enumerated values from a globally defined type in my XML schema. XmlSchema.Compile() doesn't like it. Here's the schema. <?xml version="1.0"...
2
by: Mark | last post by:
Assume you have an enumeration like PhoneType { Home, Business, Cell }. This enumeration corresponds with a lookup/dictionary table in your database like: phone_cd | phone_descr 1 ...
4
by: Marshal | last post by:
Sure... IEnumerable was inconvenient suggesting a separate class to service the enumeration, IEnumerator, and multiple operations: Current, MoveNext, Reset. (I'll warp the definition of "operation"...
1
by: Stefano G. | last post by:
I have a WSDL containing this enumeration type <xsd:simpleType name="item_type_enum"> <xsd:restriction base="xsd:string"> <xsd:enumeration value="VCD"/> <xsd:enumeration value="SVCD"/>...
27
by: Ben Finney | last post by:
Antoon Pardon wrote: > I just downloaded your enum module for python > and played a bit with it. IMO some of the behaviour makes it less > usefull. Feedback is appreciated. I'm hoping to...
3
by: Davidoff | last post by:
Hi, I parse an XML file with a XSD schema. One XmlNode has an attribute whose type is a restriction of xs:string : <xs:simpleType name="stypeDay"> <xs:restriction base="xs:string">...
0
by: news.emn.fr | last post by:
Hello, i got this attribute <xs:attribute name="jour"> <xs:simpleType> <xs:restriction base="stypeJour"> </xs:restriction> </xs:simpleType> </xs:attribute>
3
by: eXt | last post by:
For a realtime-application (half an application at least, kind of a framework) I am working with I need a event managing system and naturally it must be fast. I have defined a set of available...
3
by: muler | last post by:
hi all, After reading this excerpt from "The C# Programming Language", (By Anders) I tried to check it out. Unfortunately, I'm getting compile errors. Can anyone illustrate this with an...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
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.