I'm using two files ".h" in which there are 3 global variables with the same names.
We suppose to have these files:
When I compile C.h, I have an error cause of the conflict for same variables name.
I don't want to modify their names because They are inside public libraries (I would like to use them as they are!). This is my problem. Can someone help me? Thanks.
First, header files (".h") never get "compiled", strictly speaking, so there is no such thing as When I compile C.h
. Only .cpp
files, which of course include these header files get compiled.
Second, if your program needs to include the two header files that reference different variables that have the same name, then you probably have some bad design problem.
Third, I think (?, don't use unions very much...) that you need to define a name for a union.
Otherwise, a solution could be to define different namespaces to distinguish the two:
// A.h
namespace aa {
union myType { int m1; float m2; }
myType var1;
}
.
// B.h
namespace bb {
union myType { int m1; float m2; }
myType var1;
}
.
// C.cpp
#include "A.h"
#include "B.h"
...
aa::var1.m1 = 1;
bb::var1.m1 = 2;