473,810 Members | 2,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Visual .NET 2003 local struct definition bug

Hi !

I have some strange problem and I would like to know if it is a bug or not :

In my projects, in 2 different .cpp files, I use the same name to define a
local structure:

file1.cpp :

typedef struct TOTO
{
CString s;
GUID g;
GUID g;
};

file2.cpp :

typedef struct TOTO
{
GUID g;
CString s;
};

When I instanciate a variable toto of type TOTO in the file1.cpp, it works
but the content of variable members are strange. And when I try to define
the s variable I have a violent error.

TOTO toto; // ok ! but toto.s contains "U$...." instead of ""
toto.s = "yo"; // violent error !

I am programming in C++ and using Visual .NET 2003. The 2 files have the the
include on the precompiled header file (stdafx.h).

thanks for your help...
Mike
Nov 30 '06 #1
4 1447
On Thu, 30 Nov 2006 11:52:26 +0100, "Mike" <mi*****@nest or-tech.dot.com>
wrote:
>Hi !

I have some strange problem and I would like to know if it is a bug or not :

In my projects, in 2 different .cpp files, I use the same name to define a
local structure:

file1.cpp :

typedef struct TOTO
{
CString s;
GUID g;
GUID g;
};

file2.cpp :

typedef struct TOTO
{
GUID g;
CString s;
};
To fix the declaration errors, delete the typedefs.
>When I instanciate a variable toto of type TOTO in the file1.cpp, it works
but the content of variable members are strange. And when I try to define
the s variable I have a violent error.

TOTO toto; // ok ! but toto.s contains "U$...." instead of ""
toto.s = "yo"; // violent error !

I am programming in C++ and using Visual .NET 2003. The 2 files have the the
include on the precompiled header file (stdafx.h).
This is not a VC bug. You are violating the one definition rule (ODR) by
defining two structs with the same name but different contents in the same
scope, which here is the global namespace. The compiler is not required to
detect this, but you can fix your code by using anonymous namespaces:

file1.cpp :

namespace {

struct TOTO
{
CString s;
GUID g;
GUID g;
};

}

file2.cpp :

namespace {

struct TOTO
{
GUID g;
CString s;
};

}

--
Doug Harrison
Visual C++ MVP
Nov 30 '06 #2
Thanks for your help Doug

I believed I can do this because my structures were not declared in the
header files.
For me this error comes from the linker. The compiler cannot detect this
kind of error because the structures are not declared in the header files.
But I understand that these structures are in the global namespace, thanks
for the tip.

I have a question about this problem : I have many many .cpp files, so I
have to know all the local structures definitions names when I want to
define another one in a .cpp ?
And what happens if I use a library file instead of cpp files. If the lib
has, somewhere, a local structure definition, how can I know the name of
this structure if it is not declared in the headers ? so is it possible to
have the same kind of error ?

So, if I understand, I must always use an anonymous namespace everywhere I
want to define a local structure ?

Mike
"Doug Harrison [MVP]" <ds*@mvps.orgwr ote in message
news:t1******** *************** *********@4ax.c om...
On Thu, 30 Nov 2006 11:52:26 +0100, "Mike" <mi*****@nest or-tech.dot.com>
wrote:
>>Hi !

I have some strange problem and I would like to know if it is a bug or not
:

In my projects, in 2 different .cpp files, I use the same name to define a
local structure:

file1.cpp :

typedef struct TOTO
{
CString s;
GUID g;
GUID g;
};

file2.cpp :

typedef struct TOTO
{
GUID g;
CString s;
};

To fix the declaration errors, delete the typedefs.
>>When I instanciate a variable toto of type TOTO in the file1.cpp, it works
but the content of variable members are strange. And when I try to define
the s variable I have a violent error.

TOTO toto; // ok ! but toto.s contains "U$...." instead of ""
toto.s = "yo"; // violent error !

I am programming in C++ and using Visual .NET 2003. The 2 files have the
the
include on the precompiled header file (stdafx.h).

This is not a VC bug. You are violating the one definition rule (ODR) by
defining two structs with the same name but different contents in the same
scope, which here is the global namespace. The compiler is not required to
detect this, but you can fix your code by using anonymous namespaces:

file1.cpp :

namespace {

struct TOTO
{
CString s;
GUID g;
GUID g;
};

}

file2.cpp :

namespace {

struct TOTO
{
GUID g;
CString s;
};

}

--
Doug Harrison
Visual C++ MVP

Dec 1 '06 #3
On Fri, 1 Dec 2006 09:42:30 +0100, "Mike" <mi*****@nest or-tech.dot.com>
wrote:
>Thanks for your help Doug

I believed I can do this
You can, but you'll get the weird results you got. :)
>because my structures were not declared in the
header files.
The header/source file distinction is irrelevant. The preprocessor replaces
a #include directive with the indicated file's contents before the compiler
even sees the code. It matters not a bit if a piece of code came from a
#include directive or was part of the #including file.
>For me this error comes from the linker. The compiler cannot detect this
kind of error because the structures are not declared in the header files.
The compiler cannot detect the error because the structs are defined in
different translation units, which the compiler compiles separately.
>But I understand that these structures are in the global namespace, thanks
for the tip.

I have a question about this problem : I have many many .cpp files, so I
have to know all the local structures definitions names when I want to
define another one in a .cpp ?
That problem is solved by anonymous namespaces.
>And what happens if I use a library file instead of cpp files. If the lib
has, somewhere, a local structure definition, how can I know the name of
this structure if it is not declared in the headers ?
In general, you can't, and with anonymous namespaces, you don't have to.
>so is it possible to have the same kind of error ?
Yep.
>So, if I understand, I must always use an anonymous namespace everywhere I
want to define a local structure ?
Yep. This was a real problem before namespaces were added to the language.
While "static" worked for global functions and data, it couldn't be applied
to classes, which have linkage due to member functions (including
compiler-generated ones relevant to your example, i.e. ctors, dtors, and
assignment operator), static members, vtbls, etc.

--
Doug Harrison
Visual C++ MVP
Dec 1 '06 #4
thanks a lot for your help Doug, I will apply your advices...

"Doug Harrison [MVP]" <ds*@mvps.orgwr ote in message
news:to******** *************** *********@4ax.c om...
On Fri, 1 Dec 2006 09:42:30 +0100, "Mike" <mi*****@nest or-tech.dot.com>
wrote:
>>Thanks for your help Doug

I believed I can do this

You can, but you'll get the weird results you got. :)
>>because my structures were not declared in the
header files.

The header/source file distinction is irrelevant. The preprocessor
replaces
a #include directive with the indicated file's contents before the
compiler
even sees the code. It matters not a bit if a piece of code came from a
#include directive or was part of the #including file.
>>For me this error comes from the linker. The compiler cannot detect this
kind of error because the structures are not declared in the header files.

The compiler cannot detect the error because the structs are defined in
different translation units, which the compiler compiles separately.
>>But I understand that these structures are in the global namespace, thanks
for the tip.

I have a question about this problem : I have many many .cpp files, so I
have to know all the local structures definitions names when I want to
define another one in a .cpp ?

That problem is solved by anonymous namespaces.
>>And what happens if I use a library file instead of cpp files. If the lib
has, somewhere, a local structure definition, how can I know the name of
this structure if it is not declared in the headers ?

In general, you can't, and with anonymous namespaces, you don't have to.
>>so is it possible to have the same kind of error ?

Yep.
>>So, if I understand, I must always use an anonymous namespace everywhere I
want to define a local structure ?

Yep. This was a real problem before namespaces were added to the language.
While "static" worked for global functions and data, it couldn't be
applied
to classes, which have linkage due to member functions (including
compiler-generated ones relevant to your example, i.e. ctors, dtors, and
assignment operator), static members, vtbls, etc.

--
Doug Harrison
Visual C++ MVP

Dec 5 '06 #5

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

Similar topics

9
5547
by: soni29 | last post by:
hi, i have written the following code, still in the learning stage: #include<iostream.h> class CBox { public: // Constructor definition CBox(double lv, double bv = 1.0, double hv = 1.0) : m_Length(lv), m_Breadth(bv), m_Height(hv)
0
6146
by: Tom Lee | last post by:
Hi, I'm new to .NET 2003 compiler. When I tried to compile my program using DEBUG mode, I got the following errors in the C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7 \include\xdebug file as folows. I need help to resolve them ASAP: cl /c /nologo /MDd /W3 /Od /GR /GM /Zi /GX /D "_DEBUG" /D " WIN32" /D "_W INDOWS" /D "_WINDLL" /D "_AFXDLL" /D "_MBCS" /D "_USRDLL" /
140
7928
by: Oliver Brausch | last post by:
Hello, have you ever heard about this MS-visual c compiler bug? look at the small prog: static int x=0; int bit32() { return ++x; }
26
10891
by: Bruno Jouhier [MVP] | last post by:
I'm currently experiencing a strange phenomenon: At my Office, Visual Studio takes a very long time to compile our solution (more than 1 minute for the first project). At home, Visual Studio compiles the same solution much faster (about 10 seconds for the first project). My home computer is only marginally faster than the one I have at the office (P4 2.53 vs. P4 2.4, same amount of RAM). On the slow machine, the CPU usage is very low,...
14
2968
by: el_sid | last post by:
Our developers have experienced a problem with updating Web References in Visual Studio.NET 2003. Normally, when a web service class (.asmx) is created, updating the Web Reference will utilise the disco file to update the Corresponding proxy file and reflect the changes made to the web service. However, the results of doing this with out params is that the results seem
0
1100
by: Zychrias | last post by:
Using the following code: typedef struct { FLOAT x, y, z; // Coordinates D3DCOLOR dif; // Diffuse color } sVertex; sVertex Verts = { {-100.0f, 100.0f, 100.0f, D3DCOLOR_RGBA(255, 255, 255, 255)},
4
6540
by: kurt.kurtsmith | last post by:
I am trying to import a tlb from a COM server(exe) I wrote with the following statement: #import "C:\\em2\\EM\\src\\core\\UMOSEFax\\Debug\\UMOSEFax.tlb" no_namespace auto_search the tlh and tli are generated in the debug directory but I get the following error: Error 1 error LNK2019: unresolved external symbol "long __cdecl
0
6690
by: marathoner | last post by:
I am currently migrating my Visual C++ 6.0 applications to Visual Studio 2005. I am getting compiler errors involving the VS2005's platform SDK. When I removed directory references to that SDK, and started using the Microsoft Platform SDK for XP SP2, the errors disappeared, but I am now getting numerous warnings (macro redefinitions) as follows: StdAfx.cpp WINVER not defined. Defaulting to 0x0502 (Windows Server 2003) C:\Program...
28
2575
by: cpluslearn | last post by:
Hi, I have a local class inside a function template. I am able to wrap any type in my local class. Is it legal C++? What type of class is Local? Is it a class template or regular class? Thanks in advance. --dhina --------------------------------------------------------------------------------------------------------------------------------------------- class Foo { public:
0
9722
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
9603
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
10643
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...
0
10378
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...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
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
5550
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...
1
4333
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
3
3015
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.