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!
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 🙂