473,657 Members | 2,420 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

"declaratio n of 'foo' changes meaning from 'struct foo'"

Hello,

I am a resonably confident C programmer, but not very sure about the
dark corners of C++. Recently, I had G++ give me a strange error.

The program in question is in essence:

struct foo {
};

struct bar {
foo foo;
};

That is, I try to declare a member variable called "foo". However, G++
complains that:

Foo.C:5: error: declaration of `foo bar::foo'
Foo.C:1: error: changes meaning of `foo' from `struct foo'

Changing the line to

struct foo foo;

makes the compiler happy. However, Microsoft Visual C++ does not give
any error at all for the code.

So, which compiler is right here? If G++ is correct, what rule of C++
am I violating, and what is the reason this restriction was introduced?

Thanks in advance for any replies.

--
Vilhelm Sjöberg

Sep 10 '05 #1
5 7243

<vi************ *@gmail.com> wrote in message
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .

| The program in question is in essence:
|
| struct foo {
| };
|
| struct bar {
| foo foo;
| };
<snip>
| So, which compiler is right here? If G++ is correct, what rule of C++
| am I violating, and what is the reason this restriction was
introduced?

Which compiler is correct is not the issue here. A type is not a
variable. Perhaps the best solution involves a better identifier
convention. The last issue you want to deal with (whether you wrote the
code or you are a client of the code) is whether foo refers to the type
or to the variable.

struct Foo
{
};

struct Bar
{
Foo foo;
};

int main()
{
Bar bar;
}

I'ld rather not rely on a particular compiler's warnings or errors to
follow a set of rules as fundamental as a clear naming convention.
Sep 10 '05 #2
vi************* @gmail.com wrote:
Hello,

I am a resonably confident C programmer, but not very sure about the
dark corners of C++. Recently, I had G++ give me a strange error.

The program in question is in essence:

struct foo {
};

struct bar {
foo foo;
};

That is, I try to declare a member variable called "foo". However, G++
complains that:

Foo.C:5: error: declaration of `foo bar::foo'
Foo.C:1: error: changes meaning of `foo' from `struct foo'

Changing the line to

struct foo foo;

makes the compiler happy. However, Microsoft Visual C++ does not give
any error at all for the code.

So, which compiler is right here? If G++ is correct, what rule of C++
am I violating, and what is the reason this restriction was introduced?


I tried it on VC++ and EDG at dinkumware.com as well as at Comeau
online. None give an error, so I suspect g++ is less conformant at this
point. That said, it is *bad* style to try something like that.

Cheers! --M

Sep 11 '05 #3
On 10 Sep 2005 17:11:05 -0700, "mlimber" <ml*****@gmail. com> wrote:
Changing the line to

struct foo foo;

makes the compiler happy. However, Microsoft Visual C++ does not give
any error at all for the code.

So, which compiler is right here? If G++ is correct, what rule of C++
am I violating, and what is the reason this restriction was introduced?


I tried it on VC++ and EDG at dinkumware.com as well as at Comeau
online. None give an error, so I suspect g++ is less conformant at this
point. That said, it is *bad* style to try something like that.


I think you're right. The standard mentions other occasions where reusing an
already-declared identifier in a different context is acceptable:

7.1

2 The longest sequence of decl-specifiers that could possibly be a type name
is taken as the decl-specifier-seq of a declaration. The sequence shall be
self-consistent as described below.

[Example:
typedef char* Pc;
static Pc; // error: name missing

Here, the declaration static Pc is ill-formed because no name was specified
for the static variable of type Pc. To get a variable called Pc, a
type-specifier (other than const or volatile) has to be present to indicate
that the typedef-name Pc is the name being (re)declared, rather than being
part of the decl-specifier sequence. For another example,

void f(const Pc); // void f(char* const) (not const char*)
void g(const int Pc); // void g(const int)
—end example]

3 [Note: since signed, unsigned, long, and short by default imply int, a
type-name appearing after one of those specifiers is treated as the name being
(re)declared.

[Example:
void h(unsigned Pc); // void h(unsigned int)
void k(unsigned int Pc); // void k(unsigned int)
—end example] —end note]

So I the following is perfectly legal:

typedef foo int;

int f()
{
foo foo;
foo = 2;
}

Although it is EXTREMELY poor naming style.

-dr
Sep 11 '05 #4
[snip]
So I the following is perfectly legal:

typedef foo int;
typedef int foo;

int f()
{
foo foo;
foo = 2;
}

Although it is EXTREMELY poor naming style.
but looks fun, doesnt it ? <:

-dr

Sep 11 '05 #5
On Mon, 12 Sep 2005 00:42:18 +0200, Kyle <in*****@e.mail > wrote:
[snip]
So I the following is perfectly legal:

typedef foo int;


typedef int foo;


Why is "dyslexia" so hard to spell?

-dr
Sep 13 '05 #6

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

Similar topics

4
1819
by: Christian Christmann | last post by:
Hi, I'm reading "C++ Coding Standards" by Herb Sutter. On page 67 there's an example which I don't understand: --------------------------------- class Base{// ... virtual void Foo(int); virtual void Foo(int, int); void Foo(int, int, int); };
3
1856
by: Alexander Farber | last post by:
Hi, does anyone have an idea, why do I get the following error (I have to use g++296 on RedHat Linux as compiler): In file included from r_dir.cpp:9: r_obey.h:262: declaration of `const AreaSet &CObeyFile::AreaSet () const' r_areaset.h:197: changes meaning of `AreaSet' from `class AreaSet'
5
6326
by: Maett | last post by:
Hi. When I try to compile this piece of Code with VC.NET 2003 // code start #include <vector> template< class T > struct Marker { public:
10
20863
by: Kobu | last post by:
My question is about the use and meaning of the terms "declaration" and "definition" as it pertains to the C language. I've read sources that mix the two up when talking about such things as setting aside storage for an object, defining/declaring a struct, parts of a function, referencing an external variable in another module. sourcefile1.c ============== extern long globalfoo; /* declaration? */
8
2644
by: Mark | last post by:
Could someone provide me with details or a link on how the line of code executes underneath the hood? Assume it's executed in an ASP.NET application. string blah = ConfigurationSettings.AppSettings Once your application has called for the "foo" key once, how expensive is to call the same line again? My gut tells me that the CLR likely stores these values in memory in a string dictionary or similar. Looking up the value requires the...
8
28455
by: Mohammad Omer Nasir | last post by:
Hi, i made a structure in header file "commonstructs.h" is: typedef struct A { int i; A( ) {
30
4669
by: kj | last post by:
My book (Flanagan's JavaScript: The Definitive Guide, 5th ed.) implies on page 111 that the following two constructs are equivalent: ( x.constructor == Foo ) and ( x instanceof Foo ) The author states that the instanceof operator computes its result
0
8395
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
8732
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8503
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
8605
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...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4155
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
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
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.