免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 12243 | 回复: 3
打印 上一主题 下一主题

关于 两个头文件互相包含 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-07-14 10:20 |只看该作者 |倒序浏览
我有两个.h和.c文件,分别是queue.h ,tree.h  queue.c  tree.c  ,具体如下


/*******************************************queue.h***********************/

#ifndef queue_h
#define queue_h
#include"tree.h"





//#define element_type BinaryTree*
#define BUFFER_SIZE 15





typedef struct My_Queue

{
    //element_type Qarray[BUFFER_SIZE];
     BinaryTree* Qarray[BUFFER_SIZE];
     unsigned int front;
     unsigned int rear;
     

}Queue;


void IniQueue(Queue* Q);

void EnQueue(Queue* Q,BinaryTree* e);

void DeQueue(Queue* Q,BinaryTree* e);

#endif

/*******************************************queue.h***********************/



/*******************************************queue.c***********************/


.....

/*******************************************queue.c***********************/



/*******************************************tree.h***********************/

#ifndef tree_h
#define tree_h
#include"Queue.h"




//#define element_type char

typedef struct BinaryTree
{
     char data;
     BinaryTree*leftchild;
     BinaryTree*rightchild;

};







void Visit(BinaryTree*tree);

void PreOrder(BinaryTree*tree);

void InOrder(BinaryTree*tree);

void PostOrder(BinaryTree*tree);

void LevelOrder(BinaryTree*tree,Queue* Q);



void PrintNewLine();
BinaryTree*CreateBinaryTree();




#endif





/*******************************************tree.h***********************/





/*******************************************tree.c***********************/

......
/*******************************************tree.c***********************/





两个。h 文件互相引用了对方,一开始我就只是在每个。h文件中包含对方,但有错: fatal error C1014: 包含文件太多 : 深度 = 1024

后来 我看了下网上的一些解决方法,就在每个。h前加了宏,就像上面代码中,但这时:


1>d:\project\adt\tree.h(33) : error C2061: 语法错误 : 标识符“Queue”
1>tree.cpp
1>d:\project\adt\queue.h(21) : error C2143: 语法错误 : 缺少“;”(在“*”的前面)
1>d:\project\adt\queue.h(21) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\project\adt\queue.h(21) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>d:\project\adt\queue.h(31) : error C2061: 语法错误 : 标识符“BinaryTree”
1>d:\project\adt\queue.h(33) : error C2061: 语法错误 : 标识符“BinaryTree”侧
1>d:\project\adt\tree.cpp(5 : error C2660: “EnQueue”: 函数不接受 2 个参数
1>d:\project\adt\tree.cpp(60) : error C2660: “EnQueue”: 函数不接受 2 个参数
1>d:\project\adt\tree.cpp(61) : error C2660: “DeQueue”: 函数不接受 2 个参数



希望大家指导下

谢谢

论坛徽章:
0
2 [报告]
发表于 2007-07-14 10:24 |只看该作者
NO WAY.
用提前声明吧,例如在queue.h里不要include "tree.h"
而是声明class BinaryTree; 表示有这个类存在,从而后面的声明可以使用指向BinaryTree的指针或者引用。

论坛徽章:
0
3 [报告]
发表于 2007-07-14 11:21 |只看该作者
原帖由 emacsnw 于 2007-7-14 10:24 发表
NO WAY.
用提前声明吧,例如在queue.h里不要include "tree.h"
而是声明class BinaryTree; 表示有这个类存在,从而后面的声明可以使用指向BinaryTree的指针或者引用。





谢谢 emacsnw

问题解决了

论坛徽章:
0
4 [报告]
发表于 2007-07-14 11:28 |只看该作者
公共的东西就单独提出来访到一个地方,你这种做法是一种偷懒的做法,能解决问题,但是思路上有点混乱。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP