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 4 1431
On Thu, 30 Nov 2006 11:52:26 +0100, "Mike" <mi*****@nestor-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
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.orgwrote in message
news:t1********************************@4ax.com...
On Thu, 30 Nov 2006 11:52:26 +0100, "Mike" <mi*****@nestor-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
On Fri, 1 Dec 2006 09:42:30 +0100, "Mike" <mi*****@nestor-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
thanks a lot for your help Doug, I will apply your advices...
"Doug Harrison [MVP]" <ds*@mvps.orgwrote in message
news:to********************************@4ax.com...
On Fri, 1 Dec 2006 09:42:30 +0100, "Mike" <mi*****@nestor-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
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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) :...
|
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...
|
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;
}
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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: 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...
|
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,...
| |