1

我有这样的事情:

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

它用vs2008和intel编译器12.0编译得很好,但不能用gcc4.6.3编译,它给出了以下错误:

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’

我怎么解决这个问题?提前致谢!

4

2 回答 2

1

我认为在这种情况下(组成联合共享标识符名称的单独结构),最好不要使用匿名结构。

要么使名称在整个工会中独一无二。

其他任何事情都是自找麻烦:-)

于 2012-07-24T07:49:21.090 回答
0

如果您将程序选项传递给调用,最近的 GCC 确实接受未命名的字段,但这是标准 C99 规范的扩展。-fms-extensionsgcc

您还可以小心使用预处理器技巧,例如

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
}

然后你可以编码x.fa而不是x.u1.fa_u1 等。小心谨慎地使用预处理器技巧(记住 a#define具有完整的翻译单元范围)。在实践中,您会希望fa, u1,fa_u1 名称长且唯一。

于 2012-07-25T00:07:50.737 回答