69,369
社区成员
发帖
与我相关
我的任务
分享
假设编译器,可以正确处理,编译过程如下:
编译 main.cpp
//main.cpp
。。。。
#include "a.h" //此时处理对a.h的包含
//===> 嵌入a.h
#ifndef _A_H_ //
#define _A_H_ // _A_H_ defined.
#include "b.h"// 此时处理对b.h的包含
//==>嵌入b.h
#ifndef _B_H_
#define _B_H_ // _B_H_ defined.
#include "a.h"//==> 第二次 嵌入a.h
#ifndef _A_H_ // 1)_A_H_ defined. 所以除了配对的#endif ,后面的预编译语句,不执行;代码也不会编译了,所以没有递归。
//#define _A_H_
//........
#endif // 2) 只有 1)2) 这两个预编译语句会执行,结果是跳过中间所有代码,不编译。
struct B
{
struct A *pa;//没有前置声明,struct A不能识别,编译出错,所以struct B不能识别
}// ;这里应该有分号,不然这里或者后面,会报缺少分号错误。
#endif
struct A
{
struct B *pb; //有定义,本来是不会出错的,不过struct B不能识别,所以这里应该还是会编译出错
}// ;这里应该有分号,不然这里或者后面,会报缺少分号错误。
#endif
PS:
交叉引用的类型的头文件里面,不能缺少类型前置声明。