Reputation: 473
I have something like this:
union MyBBox3D
{
struct
{
float m_fBox[6];
float m_fCenter[3];
float m_fDiagonalLen;
float m_fNormalizeFactor;
float m_fScaling[3];
};
struct
{
float m_fMin[3];
float m_fMax[3];
float m_fCenter[3];
float m_fDiagonalLen;
float m_fNormalizeFactor;
float m_fScaling[3];
};
struct
{
float m_fMinX, m_fMinY, m_fMinZ;
float m_fMaxX, m_fMaxY, m_fMaxZ;
float m_fCenterX, m_fCenterY, m_fCenterZ;
float m_fDiagonalLen;
float m_fNormalizeFactor;
float m_fScalingX, m_fScalingY, m_fScalingZ;
};
};
It compiles well with vs2008 and intel compiler 12.0, but cant be compiled with gcc4.6.3, it gives the following errors:
In file included from Mesh/MyMeshTool.cpp:17:0:
Mesh/MyMeshTool.h:68:28: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fCenter [3]’
Mesh/MyMeshTool.h:59:28: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fCenter [3]’
Mesh/MyMeshTool.h:69:17: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fDiagonalLen’
Mesh/MyMeshTool.h:60:17: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fDiagonalLen’
Mesh/MyMeshTool.h:70:20: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fNormalizeFactor’
Mesh/MyMeshTool.h:61:20: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fNormalizeFactor’
Mesh/MyMeshTool.h:71:32: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fScaling [3]’
Mesh/MyMeshTool.h:62:32: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fScaling [3]’
Mesh/MyMeshTool.h:78:17: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fDiagonalLen’
Mesh/MyMeshTool.h:60:17: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fDiagonalLen’
Mesh/MyMeshTool.h:79:20: error: declaration of ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fNormalizeFactor’
Mesh/MyMeshTool.h:61:20: error: conflicts with previous declaration ‘float nsMeshLib::MyBBox3D::<anonymous struct>::m_fNormalizeFactor’
How can I solve this problem? Thanks in advance!
Upvotes: 1
Views: 1282
Reputation: 1
Recent GCC do accept unamed fields if you pass the -fms-extensions
program option to gcc
invocation, but this is an extension do the standard C99 specification.
You could also use preprocessor tricks with care, e.g.
union myunion {
struct {
int fa_u1;
int fb_u1;
} u1;
#define fa u1.fa_u1
#define fb u1.fb_u1
struct {
double fc_u2;
char fd_u2[8];
} u2;
#define fc u2.fc_u2
#define fd u2.fd_u2
}
Then you can code x.fa
instead of x.u1.fa_u1
etc. Use preprocessor tricks with care and caution (remember that a #define
has full translation unit scope). In practice you'll want the fa
, u1
, fa_u1
names to be long and unique.
Upvotes: 0
Reputation: 882596
I think in this case (where the separate structures making up the union share identifier names), you're probably better off not using anonymous structures.
Either that or make the names unique across the entire union.
Anything else is just asking for trouble :-)
Upvotes: 1