Fun with storage-class specifiers

Started by
4 comments, last by kingnosis 18 years, 8 months ago
I have a small program in which it has been quite convenient to use a couple of global anonymous structs. Everything compiles fine, but now that I'm at the end, I can't get the linkage right.
display.obj : fatal error LNK1179: invalid or corrupt file: duplicate COMDAT '??0<unnamed-tag>@@QAE@XZ'
I've got the anonymous structs declared in a header file. The header file is then included in all the source files that want to use the structs. I tried to use extern in front of the declarations, but that only gave me the old "unresolved external symbol". I know what I'm doing isn't quite right, but I'm not sure what to do to make it right. I appreciate any help. Thanks.
Advertisement
I never make an object of a struct in a header file, it only gets confusing. What i would do is leave the definition there and extern an object of it around all the CPP files that need it.

ace
You need an extern declaration in the header file and a non-extern definition in a single source file.

With an anonymous struct, you're in a bit of trouble, since you can't reuse the type name of the struct in the source file...
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Naming the structs was a backup plan, since I never get to use anonymous structs. [wink] But if that's the only way...

Thanks again.
Why do you have anonymous structs in a header in the first place?

I was working on a little 5 file project, and basically this is what I was trying out:

global.h
// the anon structstruct {   bool  flag;   int   value;} someData;


source1.cpp
#include "global.h"// use that data herevoid someFunc1() {   someData.flag = true;}


source2.cpp
// and use that same data herevoid someFunc2() {   if(someData.flag)      someData.value = 100;   else      someData.value = 200;}

This topic is closed to new replies.

Advertisement