|  | Class Scope Not Being Honored? |  | |
| | | Brian Neal |  |
| Posted: Wed Aug 27, 2008 1:49 am Post subject: Re: Class Scope Not Being Honored? |  |
On Aug 26, 11:28 am, Le Chaud Lapin <jaibudu...@gmail.com> wrote: [...]
| Quote: | And the compiler complains with:
error C2589: 'constant' : illegal token on right side of '::'
...because NULL has already been defined somewhere.
|
I think the #define NULL from the standard library is wreaking havoc with your enum. This is why you should never use ALL_UPPER_CASE for enum literals.
One popular proprietary realtime embedded OS vendor use to have a ton of #define macros that would periodically clobber our code until we changed our enum literal naming conventions.
Unfortunately another OS vendor had some macros in camelCase...
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | morningcoder |  |
| Posted: Wed Aug 27, 2008 1:49 am Post subject: Re: Class Scope Not Being Honored? |  |
| Quote: | It seems that I should be able to use either the class name or enum name to scope-limit the NULL so that there is no conflict.
True?
|
I think NULL is being #defined in your compilation unit. Usually it is defined as 0. So the compiler sees your code as
enum Scope {0, NODE, VINE, ZONE, SITE....}
and 0 is not allowed in enum. Try #undef NULL and it should compile. But #undef NULL in a header file may not be such a good idea.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Maciej Sobczak |  |
| Posted: Wed Aug 27, 2008 6:03 am Post subject: Re: Class Scope Not Being Honored? |  |
On 26 Sie, 18:28, Le Chaud Lapin <jaibudu...@gmail.com> wrote:
| Quote: | error C2589: 'constant' : illegal token on right side of '::'
...because NULL has already been defined somewhere.
|
Worse than that, it was probably defined to be a *macro* and that has no respect to scopes whatsoever. It just plows over the code.
Don't try to override the meaning of NULL.
-- Maciej Sobczak * LINK * LINK
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | peter koch larsen |  |
| Posted: Wed Aug 27, 2008 6:05 am Post subject: Re: Class Scope Not Being Honored? |  |
| |  | |
On 26 Aug., 18:28, Le Chaud Lapin <jaibudu...@gmail.com> wrote:
| Quote: | Hi All,
I have:
class Foo { enum Scope {NULL, NODE, VINE, ZONE, SITE....} scope() const; bool is_null() const;
}
Then I try t define:
Foo::Scope Foo::scope () const { if (this->is_null()) return Foo::NULL;
}
And the compiler complains with:
error C2589: 'constant' : illegal token on right side of '::'
...because NULL has already been defined somewhere.
It seems that I should be able to use either the class name or enum name to scope-limit the NULL so that there is no conflict.
True? No. You should know that macros don't respect anything. NULL is most |
probably a macro, and you do not have the rights to redefine NULL (the precise wording might very well be found in the C-standard). You also have posted here long enough to know that ALLUPPERCASE identifiers should be reserved for preprocessor use only - so just get rid of them.
/Peter
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Francis Glassborow |  |
| Posted: Thu Aug 28, 2008 8:15 pm Post subject: Re: Class Scope Not Being Honored? |  |
Hakusa@gmail.com wrote:
| Quote: | #undef NULL const int NULL = 0;
class Foo { enum Scope {NULL, NODE, VINE, ZONE, SITE}; };
So, I guess we are witnessing an inconsistency.
No, in the above code NULL has been declared as a global which the |
compiler is happy with in view of the #undef. You then reuse the name in a nested scope (that of class Foo) which is also fine. The original problem is the pre-processor names have no scope.
-- Note that robinton.demon.co.uk addresses are no longer valid.
[ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | gast128@hotmail.com |  |
| Posted: Thu Aug 28, 2008 8:34 pm Post subject: Re: Class Scope Not Being Honored? |  |
| Quote: | NULL is defined as a preprocessor macro in 18.1/4, so you're getting macro substitution there. Don't use NULL for your own purposes, it's essentailly a keyword.
|
Ah didn't know that.
I thought that it was an often used convention by c/c++libraries. Stroustrup's TCPL 3th edition 5.1.1 mentions that 'in C, it has been popular to define a macro NULL to represent the zero pointer'.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Le Chaud Lapin |  |
| Posted: Thu Aug 28, 2008 9:58 pm Post subject: Re: Class Scope Not Being Honored? |  |
On Aug 27, 1:03 am, Maciej Sobczak <see.my.homep...@gmail.com> wrote:
| Quote: | On 26 Sie, 18:28, Le Chaud Lapin <jaibudu...@gmail.com> wrote:
error C2589: 'constant' : illegal token on right side of '::'
...because NULL has already been defined somewhere.
Worse than that, it was probably defined to be a *macro* and that has no respect to scopes whatsoever. It just plows over the code.
|
I realized my lapse of reason just after hitting ENTER key for my original post. [Brain fried after debugging complex circuit all week.]
| Quote: | Don't try to override the meaning of NULL.
|
My enum now reads:
enum Scope {SCOPE_NULL, SCOPE_NODE, SCOPE_VINE, SCOPE_ZONE, SCOPE_SITE,...} scope() const;
Though it was a good reminder that there is always a potential for conflict with preprocessor.
-Le Chaud Lapin-
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Seungbeom Kim |  |
| Posted: Fri Aug 29, 2008 6:34 am Post subject: Re: Class Scope Not Being Honored? |  |
Le Chaud Lapin wrote:
| Quote: | My enum now reads:
enum Scope {SCOPE_NULL, SCOPE_NODE, SCOPE_VINE, SCOPE_ZONE, SCOPE_SITE,...} scope() const;
Though it was a good reminder that there is always a potential for conflict with preprocessor.
|
A better way is to name the enumerators in lowercase.
Many others have pointed out that it is advisable to reserve all-caps names for preprocessor macros only. You never know if one of the header files you include would one day define a preprocessor macro named SCOPE_NODE and break your code. If you named them in lowercase and a header defines a preprocessor macro with a same name, the blame can be put on its author, not you. :)
-- Seungbeom Kim
[ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Francis Glassborow |  |
| Posted: Sat Aug 30, 2008 3:28 pm Post subject: Re: Class Scope Not Being Honored? |  |
gast128@hotmail.com wrote:
| Quote: | NULL is defined as a preprocessor macro in 18.1/4, so you're getting macro substitution there. Don't use NULL for your own purposes, it's essentailly a keyword.
Ah didn't know that.
I thought that it was an often used convention by c/c++libraries. Stroustrup's TCPL 3th edition 5.1.1 mentions that 'in C, it has been popular to define a macro NULL to represent the zero pointer'.
|
I think you need to read what he wrote very carefully. What he actually wrote is that:
.....#define _the_ macro NULL as a pointer value...
or words to that effect. The use of 'the' rather than 'a' is highly significant in some English contexts.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | gast128@hotmail.com |  |
| Posted: Sun Aug 31, 2008 8:58 am Post subject: Re: Class Scope Not Being Honored? |  |
| Quote: | I thought that it was an often used convention by c/c++libraries. Stroustrup's TCPL 3th edition 5.1.1 mentions that 'in C, it has been popular to define a macro NULL to represent the zero pointer'.
I think you need to read what he wrote very carefully. What he actually wrote is that:
....#define _the_ macro NULL as a pointer value...
|
Not in my print.
| Quote: | or words to that effect. The use of 'the' rather than 'a' is highly significant in some English contexts.
|
Yes in other languages too. But not in this context I suppose. The discussion was that either the macro is already incoperated in the standard or not. Stroustrup mentions the 'c' language, but extrapolation by me gave me the thought that it was just a convention instead of a standard.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
|
|