|  | Class Scope Not Being Honored? |  | |
| | | Le Chaud Lapin |  |
| Posted: Tue Aug 26, 2008 4:28 pm Post subject: Class Scope Not Being Honored? |  |
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?
-Le Chaud Lapin-
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Bart van Ingen Schenau |  |
| Posted: Tue Aug 26, 2008 11:13 pm Post subject: Re: Class Scope Not Being Honored? |  |
| |  | |
Le Chaud Lapin 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. NULL is a macro that is defined by several standard header files. One of the pesky traps with macros is that they don't obey the scoping rules of the rest of C++.
This is the reason that so many coding guidelines require ALL CAPS to be used exclusively for macro names.
Your compiler is effectively seeing if (this->is_null()) return Foo::0; or something equivalent.
Bart v Ingen Schenau -- a.c.l.l.c-c++ FAQ: LINK c.l.c FAQ: LINK c.l.c++ FAQ: LINK
[ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Greg Herlihy |  |
| Posted: Tue Aug 26, 2008 11:28 pm Post subject: Re: Class Scope Not Being Honored? |  |
On Aug 26, 9:28 am, Le Chaud Lapin <jaibudu...@gmail.com> wrote:
| Quote: | class Foo { enum Scope {NULL, NODE, VINE, ZONE, SITE....} scope() const; bool is_null() const; }
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?
|
Not true - because NULL is a preprocessor macro (as the all-uppercase name would suggest). And macro substitution is performed without regard to any kind of scope.
So, after replacing NULL with the literal constant, 0, the problem with Scope's declaration in the program above - should be clear.
Greg
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | gast128@hotmail.com |  |
| Posted: Wed Aug 27, 2008 1:45 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?
-Le Chaud Lapin-
|
You are sure this is not related that your compiler-library has defined NULL (as macro)? I get a complete other error, but only with the NULL enumerate.
Btw code standards therefore reserve complete uppercase identifiers for macros.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | mos |  |
| Posted: Wed Aug 27, 2008 1:46 am Post subject: Re: Class Scope Not Being Honored? |  |
| Quote: | 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?
-Le Chaud Lapin-
|
The problem is that your preprocessor is replacing NULL with 0. So what you've passed the compiler is:
class Foo { enum Scope {0, NODE, VINE, ZONE, SITE....} scope() const; bool is_null() const;
}
Foo::Scope Foo::scope () const { if (this->is_null()) return Foo::0; }
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | red floyd |  |
| Posted: Wed Aug 27, 2008 1:46 am Post subject: Re: Class Scope Not Being Honored? |  |
On Aug 26, 9:28 am, 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.
|
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.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Erik Wikström |  |
| Posted: Wed Aug 27, 2008 1:46 am Post subject: Re: Class Scope Not Being Honored? |  |
On 2008-08-26 18:28, Le Chaud Lapin 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?
|
NULL is a #define, and they do not care about scope and such things since they are handled by the pre-processor.
-- Erik Wikström
[ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | evilissimo@googlemail.com |  |
| Posted: Wed Aug 27, 2008 1:47 am Post subject: Re: Class Scope Not Being Honored? |  |
Hi,
| Quote: | And the compiler complains with:
error C2589: 'constant' : illegal token on right side of '::'
...because NULL has already been defined somewhere.
|
NULL is a preprocessor definition (aka macros / defines) which do not obey scopes because they're processed by the preprocessor.
Regards, Vinzenz
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Pete Becker |  |
| Posted: Wed Aug 27, 2008 1:47 am Post subject: Re: Class Scope Not Being Honored? |  |
On 2008-08-26 06:28:40 -0400, Le Chaud Lapin <jaibuduvin@gmail.com> said:
| 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.
|
Not if the other definition of NULL is a macro.
-- Pete Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The Standard C++ Library Extensions: a Tutorial and Reference (www.petebecker.com/tr1book)
[ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| | | Hakusa@gmail.com |  |
| Posted: Wed Aug 27, 2008 1:47 am Post subject: Re: Class Scope Not Being Honored? |  |
| |  | |
On Aug 26, 11:28 am, Le Chaud Lapin <jaibudu...@gmail.com> wrote:
| Quote: | ...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?
|
Actually, I found two problems with your example. ONE is that NULL is already defined, but even when I changed that to NULL2, GCC tells me new types may not be defined in a return type. It really doesn't make sense this should be allowed, when you think about it. How is an instance of that type going to be created outside the function?
Declare the type. Don't use NULL. Then, declare the function. But you could always...
class Foo { enum Scope { NULL2 = NULL // More members. };
// Write enum before scope for one of the rare examples of // when it makes it more clear, other than expecting people // to notice how one is capped and the other isn't. enum Scope scope(); };
Or, if you really like NULL so much, you can write #undef NULL before the class and not use NULL in any other context. Apparently, it's a better practice to just write 0, anyway. But, by doing this, you DO have to worry about clarity and that none of your clients (which could be you) will use NULL.
Interestingly enough, the following does compile:
#undef NULL const int NULL = 0;
class Foo { enum Scope {NULL, NODE, VINE, ZONE, SITE}; };
So, I guess we are witnessing an inconsistency.
-- [ See LINK for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |
| |
| Page 1 of 2 .:. Goto page 1, 2 Next | |
|
|