引自这里,
在C中,有两种不同的类型名称空间:struct/union/enum标记名称的名称空间和typedef名称的名称空间.
name.c
$ cat name.c
#include<stdio.h>
typedef long long long2;
int long2 () {
return 4;
}
int main() {
printf("hello, world!");
return 0;
}
$ gcc name.c -o name
name.c:4: error: 'long2' redeclared as different kind of symbol
name.c:3: error: previous declaration of 'long2' was here
$
Run Code Online (Sandbox Code Playgroud)
name2.c
$ cat name2.c
#include<stdio.h>
int four() {
return 4;
}
struct dummy {
int member;
};
int main() {
struct dummy four;
}
$ gcc name2.c -o name2 …Run Code Online (Sandbox Code Playgroud) 我有两个头文件,每个都需要在另一个中定义的类型.当我尝试编译时,我收到有关未知类型名称的错误.(如果我只提供结构声明而不是定义,我会得到一个不完整类型的错误.)什么是让我能够正确分享这些结构的解决方案?
现在,我的代码看起来很像以下(想象#ifndef预处理器指令等):
<headerA.h>
#include "headerB.h"
typedef struct {
mytypeB myB;
} mytypeA;
<headerB.h>
#include "headerA.h"
typedef struct {} mytypeB;
void foo( mytypeA * myA);
Run Code Online (Sandbox Code Playgroud)