c-包括标头中定义的枚举



我摆弄枚举已经有一段时间了,我想尝试在项目中使用它们。项目结构如下:

//protocol.h

#ifndef PROTOCOL_H
#define PROTOCOL_H
enum C_C    {P_NORTH            =   0,
P_WEST              =   1,
P_SOUTH             =   2,
P_EAST              =   3};
#endif 

//其他.h

#include "protocol.h"
struct cmd {
enum C_C code : 4;
};
void make_cmd(struct cmd*, enum C_C);

此文件会触发以下错误:

field 'code' has incomplete type
'enum C_C' declared inside parameter list will not be visible outside of this definition or declaration 

//其他.c

#include "other.h"
void make_cmd(struct cmd* cmd, enum C_C code) {
cmd->code = code;
}

这会引发以下错误:

conflicting types for 'make_cmd'

我曾尝试使用typedef将枚举更改为类型,但没有成功。这种情况也发生在依赖于这种类型参数的函数定义中。

将抛出以下错误:

形式参数2的类型是不完整的

谢谢你的帮助。

只有当在另一个头中使用定义的枚举时才会发生这种情况,无论是对结构还是对函数原型。

我确实认为汇编顺序一定有问题。我在Xilinx SDK和Vitis中进行了测试,结果相同。

protocol.h包含枚举的所有定义以及整个项目中要使用的结构。我希望通过将这一个包含在其他标头中,可以在该标头的基础上构建另一个.h和另一个.c。

更新:

我已经在protocol.h中移动了结构的定义,它允许我使用枚举添加成员,而不会出现问题。我想问题是当将protocol.h导入另一个标头并尝试在那里使用枚举时,编译器在中拥有所有标头

此代码编译:

#include "other.h"
void make_cmd(struct cmd* cmd, enum C_C code) {
cmd->code = code;
}
int main(int argc, char *argv[])
{
struct cmd cmd;
make_cmd(&cmd, P_WEST);
}

如果你也#include "protocol.h",你会得到一个错误(类型重新定义(,因为它已经包含在其他.h 中了