473,385 Members | 1,893 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

please help understand this compilation error

Yan
Here is the code:

class A {};

void (A::*A) (); // Line 3

int main() {
A a; // Line 6
return 0;
}

If you remove Line 3 everything becomes trivial and compiles. However
with line 3 present Line 6 doesn't compile (on VC++ 7.1) with the
following errors:
error C2146: syntax error : missing ';' before identifier 'a'
error C2065: 'a' : undeclared identifier

Line 3 declares a pointer to member by the name A. I guess it shadows
class A and that's why Line 6 doesn't compile. Why isn't the compiler
complaining about Line 3 instead (with let's say 'redifinition' error)?
I tried Cygwin version of gcc and it fails compiling and also points
out Line 6 as the source of the error.

Thank you for your comments.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Mar 24 '06 #1
6 1872
* Yan:
Here is the code:

class A {};

void (A::*A) (); // Line 3

int main() {
A a; // Line 6
return 0;
}

If you remove Line 3 everything becomes trivial and compiles. However
with line 3 present Line 6 doesn't compile (on VC++ 7.1) with the
following errors:
error C2146: syntax error : missing ';' before identifier 'a'
error C2065: 'a' : undeclared identifier
You may find it instructive to replace your line 3 with

int A;

which yields the same effect with less extranous clutter.

Line 3 declares a pointer to member by the name A. I guess it shadows
class A and that's why Line 6 doesn't compile. Why isn't the compiler
complaining about Line 3 instead (with let's say 'redifinition'
error)?


I don't know why, but the standard has examples of declaring a struct A
and some identifier A in the same namespace, and at least one rule for
disambiguation (3.4.3.2/5), so evidently it's allowed, in the same
spirit as overloading of functions.

That was news to me, because naturally I've never done that on purpose,
and (mystery) have never encountered it before in C++.

You can disambiguate in several ways; one way that seems to work is

int main()
{
class A a;
return 0;
}

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Mar 24 '06 #2
Yan wrote:
Here is the code:

class A {};

void (A::*A) (); // Line 3

int main() {
A a; // Line 6
return 0;
}

If you remove Line 3 everything becomes trivial and compiles. However
with line 3 present Line 6 doesn't compile (on VC++ 7.1) with the
following errors:
error C2146: syntax error : missing ';' before identifier 'a'
error C2065: 'a' : undeclared identifier

Line 3 declares a pointer to member by the name A. I guess it shadows
class A and that's why Line 6 doesn't compile. Why isn't the compiler
complaining about Line 3 instead (with let's say 'redifinition' error)?
I tried Cygwin version of gcc and it fails compiling and also points
out Line 6 as the source of the error.


The A member pointer name does not shadow the A class name, rather it
hides it. Change the A class name to B and the problem becomes more
clear:

class B {};

void (B::*A) (); // Line 3

int main()
{
A a; // Line 6

}

The A on line six is the name of a member pointer - not a class - so "A
a" is a syntax error. And because the two "A"'s are in distinct name
scopes (one is a class and the other an identifier), they do not
conflict - one simply ends up hiding the other.

Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Mar 25 '06 #3
* Greg Herlihy:
Yan wrote:
Here is the code:

class A {};

void (A::*A) (); // Line 3

int main() {
A a; // Line 6
return 0;
}

If you remove Line 3 everything becomes trivial and compiles. However
with line 3 present Line 6 doesn't compile (on VC++ 7.1) with the
following errors:
error C2146: syntax error : missing ';' before identifier 'a'
error C2065: 'a' : undeclared identifier

Line 3 declares a pointer to member by the name A. I guess it shadows
class A and that's why Line 6 doesn't compile. Why isn't the compiler
complaining about Line 3 instead (with let's say 'redifinition' error)?
I tried Cygwin version of gcc and it fails compiling and also points
out Line 6 as the source of the error.


The A member pointer name does not shadow the A class name, rather it
hides it. Change the A class name to B and the problem becomes more
clear:

class B {};

void (B::*A) (); // Line 3

int main()
{
A a; // Line 6

}

The A on line six is the name of a member pointer - not a class - so "A
a" is a syntax error. And because the two "A"'s are in distinct name
scopes (one is a class and the other an identifier), they do not
conflict - one simply ends up hiding the other.


Well, please, because I'm way too lazy to solve the puzzle, where does
the C++ standard define name scopes such as "class" and "identifier"?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Mar 26 '06 #4
Alf P. Steinbach wrote:
* Greg Herlihy:
Yan wrote:
Here is the code:

class A {};

void (A::*A) (); // Line 3

int main() {
A a; // Line 6
return 0;
}

If you remove Line 3 everything becomes trivial and compiles. However
with line 3 present Line 6 doesn't compile (on VC++ 7.1) with the
following errors:
error C2146: syntax error : missing ';' before identifier 'a'
error C2065: 'a' : undeclared identifier

Line 3 declares a pointer to member by the name A. I guess it shadows
class A and that's why Line 6 doesn't compile. Why isn't the compiler
complaining about Line 3 instead (with let's say 'redifinition' error)?
I tried Cygwin version of gcc and it fails compiling and also points
out Line 6 as the source of the error.


The A member pointer name does not shadow the A class name, rather it
hides it. Change the A class name to B and the problem becomes more
clear:

class B {};

void (B::*A) (); // Line 3

int main()
{
A a; // Line 6

}

The A on line six is the name of a member pointer - not a class - so "A
a" is a syntax error. And because the two "A"'s are in distinct name
scopes (one is a class and the other an identifier), they do not
conflict - one simply ends up hiding the other.


Well, please, because I'm way too lazy to solve the puzzle, where does
the C++ standard define name scopes such as "class" and "identifier"?


I consulted the grammar summary in Appendix A and came up "class-name"
but no special term for a variable's name - only "identifier". A
class-name is also an identifier - so perhaps "name" or "variable name"
would been a better choice. Especially since §3.1/4 defines a "name"
as "a use of an identifier (2.10) that denotes an entity or label
(6.6.4, 6.1)." §3.1/2 defines "entity" as "a value, object, subobject,
base class subobject, array element, variable, function, instance of a
function, enumerator, type, class member, template, or namespace."

A name therefore is distinct from a user-declared "keyword" which can
be any of a "class-name, enum-name, template-name, typedef-name,
namespace-name, original-namespace-name or a namespace-alias."
(summarized in Appendix A.1)

The formal definition of naming hiding also distinguishes between
different name - here again I probably did not pick the best word to
use (scope) perhaps "category" or "type" - would have been more clear:

"A class name (9.1) or enumeration name (7.2) can be hidden by the name
of an object, function, or enumerator declared in the same scope. If a
class or enumeration name and an object, function, or enumerator are
declared in the same scope (in any order) with the same name, the class
or enumeration name is hidden wherever the object, function, or
enumerator name is visible." §3.3.7/2

Greg
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Mar 26 '06 #5
Alf P. Steinbach wrote:
* Yan:
Here is the code: class A {}; void (A::*A) (); // Line 3 int main() {
A a; // Line 6
return 0;
} If you remove Line 3 everything becomes trivial and
compiles. However with line 3 present Line 6 doesn't compile
(on VC++ 7.1) with the following errors:
error C2146: syntax error : missing ';' before identifier 'a'
error C2065: 'a' : undeclared identifier
You may find it instructive to replace your line 3 with int A; which yields the same effect with less extranous clutter. Line 3 declares a pointer to member by the name A. I guess
it shadows class A and that's why Line 6 doesn't compile.
Why isn't the compiler complaining about Line 3 instead
(with let's say 'redifinition' error)?

I don't know why, but the standard has examples of declaring a
struct A and some identifier A in the same namespace, and at
least one rule for disambiguation (3.4.3.2/5), so evidently
it's allowed, in the same spirit as overloading of functions.
History. And C compatibility (in a way that is still very
important today). Basically, in C, structure names are in a
different namespace than other symbols, and something like:

struct A {} ;
int A ;

is perfectly legal. This causes no problems in C (and requires
no special rules) because you cannot use the first A as the name
of a type. You must write "struct A".

In C++, of course, after the first statement, A is the name of a
type, and can be used as such. And A is defined in the current
scope; logically, the second line would be illegal. However, if
you've got a C header which uses something like that, you still
want to be able to compile it in C++, by just wrapping the
header in ` extern "C" {...} '. So we get some very special,
and very strange rules, just to allow common headers for C and
C++.
That was news to me, because naturally I've never done that on
purpose, and (mystery) have never encountered it before in
C++. You can disambiguate in several ways; one way that seems to work is int main()
{
class A a;
return 0;
}


Right. The best solution, of course, is not to create the
ambiguity to begin with. If you're stuck with a C header which
has it, however, then the rule is that if there are two symbols,
one the name of a class, and one not, the one that is not the
name of a class is found except after the keywords class and
struct.

Personally, in such cases, I think I'd prefer using a typedef:

typedef class A AA ;

But it's arguable. If you're programming against a known API,
then struct A is presumably known to all of the programmers,
where as AA is an unknown type for them.

--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Mar 27 '06 #6
* kanze:
Alf P. Steinbach wrote:
You can disambiguate in several ways; one way that seems to work is

int main()
{
class A a;
return 0;
}


Right. The best solution, of course, is not to create the
ambiguity to begin with. If you're stuck with a C header which
has it, however, then the rule is that if there are two symbols,
one the name of a class, and one not, the one that is not the
name of a class is found except after the keywords class and
struct.


Or perhaps 'enum', when the type is an enum type(but as stated earlier,
or at least as should have been stated earlier, I really don't feel like
finding the relevant small cryptic statements hidden in the standard).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Mar 27 '06 #7

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

Similar topics

26
by: Michael Strorm | last post by:
Hi! I posted a message a while back asking for project suggestions, and decided to go with the idea of creating an adventure game (although it was never intended to be a 'proper' game, rather an...
4
by: Lloyd Sheen | last post by:
So MS where is the error that is reported on last line, says 1 failed. No indication as to what failed. VS 2003, (reinstalled more times than should have been). ------ Build started: Project:...
4
by: Programmer | last post by:
Hi everyone Well here is my problem I hope you can help me
2
by: James Zhuo | last post by:
Hi all I've been getting the following compilation error. I should explain the background of the project that i am taking over. This is a project that has been developed by someone else a while...
0
by: James Zhuo | last post by:
Hi all I've been getting the following compilation error. I should explain the background of the project that i am taking over. This is a project that has been developed by someone else a...
3
by: Kris van der Mast | last post by:
Hi, I've created a little site for my sports club. In the root folder there are pages that are viewable by every anonymous user but at a certain subfolder my administration pages should be...
22
by: MLH | last post by:
I would like to test some of this code in the debug window... Option Compare Database Option Explicit Private Sub Command0_Click() #If Win32 Then MsgBox "Hey! It's WIN32." #End If End Sub
4
by: Brad Isaacs | last post by:
I am working with ASP.NET 2.0 and using an SQL Server 2000 database. I am using Visual Studio 2005 and developing on my Local machine. I am working with Login controls ASP.Configuration, I...
2
by: =?Utf-8?B?UGF1bCBCdXp6YSwgb2xkc3RlciB1c2luZyBuZXcg | last post by:
I recently implemented error processing on my .net website, where I send myself an email from global.asax on an application error. In the last two days I have gotten a "The file '/detail.aspx'...
1
by: zest4 | last post by:
Getting this error, please help what has to be confugured on Win 2003 server? Server Error in '/' Application. -------------------------------------------------------------------------------- ...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
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...

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.