ç递归头文件包含的问题? [英] C recursive header file inclusion problem?
本文介绍了ç递归头文件包含的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设你有在2头文件中定义如下图所示的相关结构:
Suppose you have to related structures defined in 2 header files like below:
A.H内容:
#include b.h
typedef struct A
{
B *b;
} A;
b.h内容:
#include a.h
typedef struct B
{
A *a;
} B;
在这种这种情况下,这种递归包容是一个问题,但2结构必须指向其它结构,如何做到这一点?
In such this case, this recursive inclusion is a problem, but 2 structures must point to other structure, how to accomplish this?
推荐答案
不要使用#include当正向声明就足够了。
Don't use an #include when a forward declaration would suffice
这会意味着:
A.H内容:
typedef struct B B;
typedef struct A
{
B *b;
} A;
b.h内容:
typedef struct A A;
typedef struct B
{
A *a;
} B;
如果您preFER的东西多了一点安全(但更长的时间来编译),你可以这样做:
If you prefer something a bit safer (but longer to compile) you can do this:
A.H内容:
#pragma once
typedef struct A A;
#include "B.h"
typedef struct A
{
B *b;
} A;
b.h内容:
#pragma once
typedef struct B B;
#include "A.h"
typedef struct B
{
A *a;
} B;
这篇关于ç递归头文件包含的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文