c - 错误 : Redefinition of union

标签 c struct compiler-errors unions

在下面的代码中:

typedef struct
{
    union U
        {
            int a;
            char b;
        }U1;
}A;

typedef struct
{
    union U
        {
            int a;
            char b;
        }U1;
}B;

编译器给出错误“[Error] redefinition of 'union U'”。 但这些 union 是不同结构的成员。 所以一般不会有变量名干扰的机会。 那么这个错误的原因是什么?

最佳答案

那里没有将名称分离到 namespace 中,您实际上正在尝试重新定义。

片段:

typedef struct { union U { int a; char b; } U1; } A;

这里没有显着差异:

union U { int a; char b; };
typedef struct { union U U1; } A;

你可以通过简单地为你的类型引入人工命名空间来解决它:

typedef struct { union AU { int a; char b; } U1; } A;
typedef struct { union BU { int a; char b; } U1; } B;

或者,如果 union 意味着(并将保持)相同的类型,只需定义它一次:

union U { int a; char b; };
typedef struct { union U U1; } A;
typedef struct { union U U1; } B;

关于c - 错误 : Redefinition of union,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21569663/

相关文章:

compiler-errors - 找不到org.mule.module.client.MuleClient编译错误

compiler-errors - 如何抑制IAR EW 8.1中的Pe070错误?

c - STM32 串行 DMA - 查找流的开头

C 程序卡住字长大于 6

objective-c - 在 forwar 类对象 CALayer 中找不到属性 anchorPoint

c++ - Valgrind 在通过指向结构的指针访问结构成员时提示无效读取

pointers - 我应该在 struct 字段上还是在 struct 上创建指针?走

c - 错误: assignment to expression with array type while using selection-sort

c++ - 混合 C/C++ 源的 Makefile

java - 在 Java 中捕获 Unresolved 编译错误