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次に、etcの代わりにコーディングできますx.u1.fa_u1 。プリプロセッサのトリックを注意して使用してください(#define完全な変換ユニットスコープがあることに注意してください)。実際には、、、のfa名前u1fa_u1 長くて一意である必要があります。

于 2012-07-25T00:07:50.737 に答える