Chinaunix首页 | 论坛 | 博客
  • 博客访问: 36749
  • 博文数量: 25
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 258
  • 用 户 组: 普通用户
  • 注册时间: 2014-02-08 15:12
文章分类

全部博文(25)

文章存档

2015年(2)

2014年(23)

我的朋友

分类: C/C++

2014-02-10 22:50:55

问题????

add.h

点击(此处)折叠或打开

  1. #include "mymath.h"
  2. int add(int x, int y);

subtract.h

点击(此处)折叠或打开

  1. #include "mymath.h"
  2. int subtract(int x, int y);

main.cpp

点击(此处)折叠或打开

  1. #include "add.h"
  2. #include "subtract.h"
当我们有add.h,它带来了两个mymath.h和原型的add()。
当我们有subtract.h,它带来两个mymath.h(再次)和原型减去()。
因此,mymath.h的所有内容将被包含了两次,这将导致编译器报错。
解决方法:

    Header guard 直译是头文件保护

    在定义头文件是使用 头文件保护
例:

点击(此处)折叠或打开

  1. #ifndef SOME_UNIQUE_NAME_HERE
  2. #define SOME_UNIQUE_NAME_HERE
  3.  
  4. // your declarations here
  5.  
  6. #endif



阅读(263) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~