两个必须包含的头文件要检查一个同名的预处理器常量

最近看C++ primer

关于#ifndef防止头文件的重复处理有这么一句话,"只要不存在两个必须包含的头文件要检查一个同名的预处理器常量这样的情形这个策略就能够很好地运作 ",比较不理解,特列两个例子.

 

/* test1.h */
#ifndef TEST1_H
#define  TEST1_H
#include
< iostream >
using   namespace  std;

#define  DEBUG 6

#endif


/* test2.h */
#ifndef TEST2_H
#define  TEST2_H
#include
< iostream >
using   namespace  std;

#define  DEBUG 7 

#endif


/* test.cpp */
#include
< iostream >
using   namespace  std;
#include 
" test1.h "
#include 
" test2.h "

int  main()
{
   cout
<<DEBUG;
   
return 1;
}

 

此时结果为 6,test2.h并未执行.

 

另外一个例子:

/* test1.h */
#ifndef TEST1_H
#define  TEST1_H
#include
< iostream >
using   namespace  std;

#define  DEBUG1 1

#endif

/* test2.h */
#ifndef TEST1_H
#define  TEST2_H
#include
< iostream >
using   namespace  std;

#define  DEBUG2 2 

#endif


/* test.cpp */
#include
< iostream >
using   namespace  std;
#include 
" test1.h "
#include 
" test2.h "

int  main()
{
cout
<<DEBUG1<<endl;
cout
<<DEBUG2<<endl;
}

 

此时就有了问题,编译时会有问题:

error C2065: 'DEBUG2' : undeclared identifier.

个人觉得上面两个例子应该和此命题相关.

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值