473,785 Members | 2,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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++.m oderated. First time posters: Do this! ]

Mar 24 '06 #1
6 1914
* 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++.m oderated. 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++.m oderated. 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++.m oderated. 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++.m oderated. 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 objektorientier ter Datenverarbeitu ng
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++.m oderated. 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++.m oderated. 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
2942
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 excuse to write- and learn- some C++). To cut a long story short, I wrote a fair chunk of it, but realised that it's... not very good. Okay, it's my first "proper" C++ program, so that's no big deal, but I don't want to waste more time working...
4
1308
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: SQLEditor, Configuration: Debug .NET ------ Preparing resources... Updating references... Performing main compilation... Building satellite assemblies...
4
447
by: Programmer | last post by:
Hi everyone Well here is my problem I hope you can help me
2
1806
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 ago using dreamweaver, I am basically trying to migrate the project into a Visual Studio .NET environment before I start enhancing it. Any suggestions on where the source of error may be would be greatly appreciated.
0
329
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 ago using dreamweaver, I am basically trying to migrate the project into a Visual Studio .NET environment before I
3
4871
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 protected by forms authentication. When I create forms authentication at root level it works but when I move my code up to the subfolder I get this error: Server Error in '/TestProjects/FormsAuthenticationTestingArea' Application.
22
6323
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
2216
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 wanted to move my work and needed to place it on the server. Using VS 2005 , went to BUILD -Publish Web Site Checked the box for :: Alow this precompiled site to be updatable.
2
2015
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' does not exits" type error--with two different file names. In both cases, I searched my entire source for the '/detail.aspx' reference and it doesn't exist. What could be causing this error? Text of error email I sent to myself follows.... ...
1
1143
by: zest4 | last post by:
Getting this error, please help what has to be confugured on Win 2003 server? Server Error in '/' Application. -------------------------------------------------------------------------------- Security Exception Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust...
0
9646
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
9483
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
10346
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...
1
10096
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
9956
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7504
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
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
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.