enums

N

Noah Roberts

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?
 
M

Mike Wahler

Noah Roberts said:
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
 
P

Peter Koch Larsen

Noah Roberts said:
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
 
V

Victor Bazarov

Noah said:
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
 
N

Noah Roberts

Mike said:
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::vector< std::string >& hot_fixes) const;
bool check_sp(std::string &sp_exact_match) const;
unsigned int version() const;
};

class WinChecker_GTNTSIX
{
OSVERSIONINFOEX os_version_info;

enum { WINNT, WIN2K, WINXP, WIN2003 };

public:
WinChecker_GTNTSIX();

bool sp_equal_or_greater_than(unsigned 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_greater_than(float)' cannot have `const' method
qualifier
.../include/WinChecker.h:39: non-member function `bool
check_hotfixes(std::vector said:
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::WinChecker()
{
os_version_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

if (!GetVersionEx(&os_version_info))
{
throw std::runtime_error("Could not get version info of
Windows.");
}
}

WinChecker_GTNT6::WinChecker_GTNT6()
{
os_version_info.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

if (!GetVersionEx(&os_version_info))
{
throw std::runtime_error("Could not get extended version info of
Windows.");
}
}

std::string WinChecker::sp_version() const
{
return static_cast<char*>(os_version_info.szCSDVersion);
}

bool WinChecker::check_sp(std::string &sp_exact_match) const
{
return sp_exact_match ==
static_cast<char*>(os_version_info.szCSDVersion);
}

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++,f77,objc
--disable-win32-registry --disable-shared --enable-sjlj-exceptions
Thread model: win32
gcc version 3.2.3 (mingw special 20030504-1)

Thanks.
 
V

Victor Bazarov

Noah said:
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
 
N

Noah Roberts

Victor said:
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.
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top