LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1245|回复: 10

多个程序文件造成重复include的问题

[复制链接]
发表于 2005-5-15 16:00:52 | 显示全部楼层 |阅读模式
共有list.h、queue.h、queue.cpp、stack.h、stack.cpp、main.cpp这些文件

3个cpp共同生成可执行文件

queue.h与stack.h都#include "list.h"

main.cpp里#include "queue.h"与#include "stack.h"

编译的时候报告错误:

In file included from queue.h:1,
               from main.cpp:4:
list.h:2: error: redefinition of `struct list'
list.h:2: error: previous definition of `struct list'
make: *** [doit] 错误 1

怎么解决呢?
发表于 2005-5-15 16:08:33 | 显示全部楼层
  1. #ifndef SOMETHING
  2. #define SOMETHING

  3. /* contents of your header file */

  4. #endif
复制代码
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-15 16:22:31 | 显示全部楼层
具体怎么加啊?
回复 支持 反对

使用道具 举报

发表于 2005-5-15 16:24:34 | 显示全部楼层
Replace 'SOMETHING' with an unique ID and '/* contents of your header file */' with whatever you want to write in your header file.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-15 16:26:26 | 显示全部楼层
还是不太懂

在哪个文件加呢?

加在什么位置呢?

thx
回复 支持 反对

使用道具 举报

发表于 2005-5-15 16:34:03 | 显示全部楼层
Here is an example:
  1. #ifndef EXAMPLE_H__
  2. #define EXAMPLE_H__

  3. struct example {
  4.     int value;
  5.     struct example *next;
  6. };

  7. #endif  /* EXAMPLE_H__ */
复制代码

As you see, the first time including this file will define a preprocessor macro EXAMPLE_H__, which will prevent the structure definition being included for the second time.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-15 16:42:17 | 显示全部楼层
thx

ok le

any principles here?
回复 支持 反对

使用道具 举报

发表于 2005-5-15 16:50:43 | 显示全部楼层
You should choose the preprocessor macro (EXAMPLE_H__ in the above example) carefully to avoid name duplicating. What's more, you'd better add comment like '/* EXAMPLE_H__ */ after the last #endif since it will be a bit harder to find the matched #ifndef if the header file is very long.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2005-5-15 17:01:40 | 显示全部楼层
关于这个,C/C++有没有专有的名词?

就象继承、派生等

thx
回复 支持 反对

使用道具 举报

发表于 2005-5-15 17:02:54 | 显示全部楼层
No. It is just a skill.
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表