避免“重新定义typedef”定义结构时的警告 [英] Avoiding "redefinition of typedef" warnings when defining structs

查看:266
本文介绍了避免“重新定义typedef”定义结构时的警告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一些引用彼此的结构,并在使用它们之前对结构进行了类型定义,因此每个结构都能知道其他结构(没有这种情况下会产生编译错误)。不知道这是必要的,还是正确的。

I'm defining some structs which reference eachother, and typedef'ing the structs before using them, so each struct is 'aware' of the others (was getting compilation errors without this). Not sure if this is necessary, or correct.

现在使用gcc编译时,我得到了重新定义typedef警告。什么是正确的方式去做这件事?

Now when compiling with gcc, I'm getting "redefinition of typedef" warnings. What's the correct way to go about this?

typedef struct a A;
typedef struct b B;
typedef struct c C;

struct a {
    B* list;
    A* parent;
};

struct b {
    A* current;
    B* next;
};

struct c {
    A* current;
    A* root;
};

更新:
哑巴,错误的复制粘贴导致这个头文件被包含在另一个文件中两次。我是C新手,并且认为它必须与文件中的结构两次有关。感谢@Kevin Ballard提出的问题。

UPDATE: Dumb, bad copy-pasting resulted in this header being included twice in another file. I'm new to C and thought it must have something to do with having the structs in the file twice. Thanks @Kevin Ballard for the heads up.

推荐答案

这是为什么需要头文件/ p>

This is a good example of why header/include guards are needed:

#ifndef MY_HEADER_FILE
#define MY_HEADER_FILE

typedef struct a A;
typedef struct b B;
/* ... */

#endif

这篇关于避免“重新定义typedef”定义结构时的警告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆