473,803 Members | 3,758 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

enums

I am having an interesting compilation error that makes me wonder if
enums can conflict with each other.

For example:

class A
{
enum X { TEST, TEST2 };
public:
....
};

class B
{
enum Y { TEST2 };
public:
....
};

My interpretation of that would be that there is a type A::X which can
contain the values A::TEST or A::TEST2 and type B::Y that can contain
B::TEST2. However, my compiler is complaining about stuff related to
the line that would be "enum Y..."

errors include:
parse error before numeric constant.
missing ';' before right brace.
parse error before 'public'
....
various others probably caused by the above...

If I change B's TEST2 to TEST2t it works. It was my understanding that
the enums would not conflict because they have totally different scope
(at least in C++). Do I need to only use const ints then?

Jul 23 '05 #1
6 2053

"Noah Roberts" <nr******@stmar tin.edu> wrote in message
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
I am having an interesting compilation error that makes me wonder if
enums can conflict with each other.

For example:

class A
{
enum X { TEST, TEST2 };
public:
...
};

class B
{
enum Y { TEST2 };
public:
...
};

My interpretation of that would be that there is a type A::X which can
contain the values A::TEST or A::TEST2 and type B::Y that can contain
B::TEST2. However, my compiler is complaining about stuff related to
the line that would be "enum Y..."

errors include:
parse error before numeric constant.
missing ';' before right brace.
parse error before 'public'
...
various others probably caused by the above...

If I change B's TEST2 to TEST2t it works. It was my understanding that
the enums would not conflict because they have totally different scope
(at least in C++). Do I need to only use const ints then?


What you posted above (once I removed the "..." lines) compiled
OK for me. Perhaps you should post the real code that gave
the errors.

-Mike
Jul 23 '05 #2

"Noah Roberts" <nr******@stmar tin.edu> skrev i en meddelelse
news:11******** **************@ l41g2000cwc.goo glegroups.com.. .
I am having an interesting compilation error that makes me wonder if
enums can conflict with each other.

For example:

class A
{
enum X { TEST, TEST2 };
public:
...
};

class B
{
enum Y { TEST2 };
public:
...
};

My interpretation of that would be that there is a type A::X which can
contain the values A::TEST or A::TEST2 and type B::Y that can contain
B::TEST2. However, my compiler is complaining about stuff related to
the line that would be "enum Y..."

errors include:
parse error before numeric constant.
missing ';' before right brace.
parse error before 'public'
...
various others probably caused by the above...

If I change B's TEST2 to TEST2t it works. It was my understanding that
the enums would not conflict because they have totally different scope
(at least in C++). Do I need to only use const ints then?

Your code looks fine. It must be something you left out that caused the
problems. Perhaps a missing } (which could explain the error)?

/Peter
Jul 23 '05 #3
Noah Roberts wrote:
I am having an interesting compilation error that makes me wonder if
enums can conflict with each other.

For example:

class A
{
enum X { TEST, TEST2 };
public:
...
};

class B
{
enum Y { TEST2 };
public:
...
};

My interpretation of that would be that there is a type A::X which can
contain the values A::TEST or A::TEST2 and type B::Y that can contain
B::TEST2.
Well, the more precise description would be that there is a type A::X
and values A::TEST and A::TEST2 that have type A::X, and there is type
B::Y and a value B::TEST2 of that type. Generally, by using casts you
may stuff any other value into A::X or B::Y that is supported by the
*underlying* integral type. For example, 2 or 3 or 42.
However, my compiler is complaining about stuff related to
the line that would be "enum Y..."
Seems like your compiler is buggy. Have you tried any other compilers?
errors include:
parse error before numeric constant.
missing ';' before right brace.
parse error before 'public'
...
various others probably caused by the above...

If I change B's TEST2 to TEST2t it works. It was my understanding that
the enums would not conflict because they have totally different scope
(at least in C++). Do I need to only use const ints then?


Probably, unless you can change/upgrade your compiler.

V
Jul 23 '05 #4

Mike Wahler wrote:
What you posted above (once I removed the "..." lines) compiled
OK for me. Perhaps you should post the real code that gave
the errors.
Ok. This is not entirely standard code, but the Q is.

#ifndef WINCHECKER_H
#define WINCHECKER_H

/*
* Checks information on the version of Windows and provides
* access points to test SP, and HotFixes.
*/

#include <windows.h>
#include <winnt.h>

#include <vector>
#include <string>

class WinChecker
{
OSVERSIONINFO os_version_info ;

enum { WIN95, WIN98, WINME, WINNT3, WINNT4, WIN2K, WINXP, WIN2003 };

public:
WinChecker();
std::string sp_version() const;
bool check_hf(std::v ector< std::string >& hot_fixes) const;
bool check_sp(std::s tring &sp_exact_match ) const;
unsigned int version() const;
};

class WinChecker_GTNT SIX
{
OSVERSIONINFOEX os_version_info ;

enum { WINNT, WIN2K, WINXP, WIN2003 };

public:
WinChecker_GTNT SIX();

bool sp_equal_or_gre ater_than(unsig ned float sp) const;
bool check_hotfixes( std::vector< std::string >& hot_fixes) const;
unsigned int version() const;
};

#endif
The output of compiler:

$ g++ -c -I../include WinChecker.cpp
In file included from WinChecker.cpp: 1:
.../include/WinChecker.h:33 : parse error before numeric constant
.../include/WinChecker.h:33 : missing ';' before right brace
.../include/WinChecker.h:35 : parse error before `public'
.../include/WinChecker.h:38 : short, signed or unsigned invalid for `sp'
.../include/WinChecker.h:38 : non-member function `bool
sp_equal_or_gre ater_than(float )' cannot have `const' method
qualifier
.../include/WinChecker.h:39 : non-member function `bool
check_hotfixes( std::vector<std ::string, std::allocator< std::string>&)'

cannot have `const' method qualifier
.../include/WinChecker.h:40 : non-member function `unsigned int
version()' cannot
have `const' method qualifier
.../include/WinChecker.h:41 : parse error before `}' token
WinChecker.cpp: 15: syntax error before `::' token

WinChecker.cpp to be complete:

#include "WinChecker .h"

#include <stdexcept>

WinChecker::Win Checker()
{
os_version_info .dwOSVersionInf oSize = sizeof(OSVERSIO NINFO);

if (!GetVersionEx( &os_version_inf o))
{
throw std::runtime_er ror("Could not get version info of
Windows.");
}
}

WinChecker_GTNT 6::WinChecker_G TNT6()
{
os_version_info .dwOSVersionInf oSize = sizeof(OSVERSIO NINFOEX);

if (!GetVersionEx( &os_version_inf o))
{
throw std::runtime_er ror("Could not get extended version info of
Windows.");
}
}

std::string WinChecker::sp_ version() const
{
return static_cast<cha r*>(os_version_ info.szCSDVersi on);
}

bool WinChecker::che ck_sp(std::stri ng &sp_exact_match ) const
{
return sp_exact_match ==
static_cast<cha r*>(os_version_ info.szCSDVersi on);
}

Commenting out the OSVERSIONINFOEX doesn't get rid of the parse error
on #33. There is a design error in that I will be making the enums
public, but this is what fails to compile.

Compiler info:
$ g++ -v
Reading specs from c:/mingw/bin/../lib/gcc-lib/mingw32/3.2.3/specs
Configured with: ../gcc/configure --with-gcc --with-gnu-ld
--with-gnu-as --host=mingw32 --target=mingw32 --prefix=/mingw
--enable-threads --disable-nls --enable-languages=c++,f 77,objc
--disable-win32-registry --disable-shared --enable-sjlj-exceptions
Thread model: win32
gcc version 3.2.3 (mingw special 20030504-1)

Thanks.

Jul 23 '05 #5
Noah Roberts wrote:
Mike Wahler wrote:

What you posted above (once I removed the "..." lines) compiled
OK for me. Perhaps you should post the real code that gave
the errors.

Ok. This is not entirely standard code, but the Q is.

#include <windows.h>
#include <winnt.h>

[...]
enum { WIN95, WIN98, WINME, WINNT3, WINNT4, WIN2K, WINXP, WIN2003 };
[...]
enum { WINNT, WIN2K, WINXP, WIN2003 };


Are you sure *none* of these symbols (WIN<blah>) is a macro expanding to
some kind of a integer literal? Look at the preprocessor output and
search for your class definition and see what the preprocessor turns it
into.

V
Jul 23 '05 #6

Victor Bazarov wrote:
Are you sure *none* of these symbols (WIN<blah>) is a macro expanding to some kind of a integer literal?


No. WINNT apparently is.

Thanks. Glad to know that I at least knew the language even if I did
fall into a silly trap. I thought I had used the same ones in both
classes, but I noticed WINNT was WINNT4 in the upper...that is why it
didn't bitch also.

Jul 23 '05 #7

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

Similar topics

13
9558
by: SpaceCowboy | last post by:
I recently got into a discussion with a co-worker about using enums across a dll interface. He wanted to use chars instead, argueing that depending on compiler settings the size of an enum could change and lead to memory corruption. I didn't see how this was possible. He claims that if a dll built for a program is built with different compiler settings than the launcher program, the enum size could change. The only way I could figure...
2
2083
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't seem to find an appropriate Reflection method to access Enums within a class I would like to loop through the Enums in the 'Foo' Class retrieve the Enums 'Car' and 'House Public Class Fo Public Enum Ca For Chevrole Toyot
27
2747
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template <typename Enum> class smart_enum { public: typedef Enum enum_type;
2
1796
by: SpotNet | last post by:
Hello CSharpies, Can Enums in C# be UInt32 Types, and not just Int32 Types. I have a lot of constant declarations that are UInt32 that I want to put in Enums to ease the use of Intellisence. I have them in structs for now, I thought I'd ask. Would IntPtr Enums push the bounds of a stupid question? Regarding a post a bit further down by Tom; ' What would you like to change in C# and .NET?'
4
5594
by: Martin Pritchard | last post by:
Hi, I'm working on a project that historically contains around 40 enums. In the database various fields refer to the int values of these enums, but of course ref integrity is not enofrced and when looking at the db we can't tell what the value in a field represents. The other problem is that our enums are currently all stored in a single class, which means that because of no visibility constraints the enums are often used out of context...
2
1999
by: Faisal | last post by:
Can anyone tell me if it is possible to enumerate through all the Enums within a class . I have a class with many Enums and would like to accees the Enums through an array/collection etc. I can't seem to find an appropriate Reflection method to access Enums within a class Thanks! --- Posted using Wimdows.net Newsgroups - http://www.wimdows.net/newsgroups/
2
2884
by: Simon Elliott | last post by:
I have some legacy C++ code which requires some enums to be 1 or 2 bytes in size. I'd ideally like to be able to specify that a few carefully selected enums are a particular size. By default, g++ seems to create enums of 4 bytes in length, unless I use the -fshort-enums option. I don't much want to use this all or nothing approach in case it breaks library code etc, and there's no guarantee that other compilers have a comparable...
11
12562
by: Marc Gravell | last post by:
This one stumped me while refactoring some code to use generics... Suppose I declare an enum MyEnum {...} Is there a good reason why MyEnum doesn't implement IEquatable<MyEnum> ? Of course, I can cast a MyEnum instance down to the int / short whatever (since int implements IEquatable<int>), but I don't like doing that, as it feels a bit messy, and I am then propegating the things that know what the base represenation is...
4
3001
by: Jon Slaughter | last post by:
is there a simple way to "step" through enums? I have a button that I want to click and have it "cycle" through a set of states defined by enums but the only way I can think of doing this "properly" yet "ugly" is to test the state for each state. I know that enumes are not ordered but since they are "stored" as numbers they have an inherent ordering which can be used. I don't really care about the ordering though but just the ability to...
13
5018
by: Bob | last post by:
Hi, Can someone explain why you can't declare enums in an interface? The compiler says "interfaces can't declare types" Ignoring the syntax implications it seems to me that you should be able to declare that an enum exists. i.e. It is within the 'spirit' of an interface to state that it exists. The implementing class will provide the members. e.g. I have a code library that performs a function. It exposes two interfaces which allow for...
0
9700
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
9564
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10546
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
10310
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...
1
10292
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9121
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
7603
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
6841
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();...
2
3796
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.