Visual .NET 2003 local struct definition bug

M

Mike

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
 
D

Doug Harrison [MVP]

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;
};

}
 
M

Mike

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
 
D

Doug Harrison [MVP]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top