头文件问题

darcymei 2003-10-09 11:13:29
//Node.h
//----------------------------------------------
#include <iostream>
using namespace std;

enum Error_code{success,overflow,underflow};

template<typename type>
struct Node
{
type entry;
Node * next;
Node();
Node(type,Node * add_on=NULL);
};

template<typename type>
inline Node<type>::Node()
{
next=NULL;
}

template<typename type>
inline Node<type>::Node(type item, Node * add_on)
{
entry=item;
next=add_on;
}
//--------------------------------------------------------------

//Stack.h
//--------------------------------------------------------------
template<typename type>
class Queue
{
public:
Queue();
bool empty()const;
bool full()const;
int size()const;
void clear();
Error_code append(const type item);
Error_code serve();
Error_code retrieve(type& item)const;
Error_code serve_and_retrieve(type &item);
void prin()const;
~Queue();
Queue(const Queue &);
void operator = (const Queue &);
protected:
Node<type> * tail;
};
//-----------------------------------------------------------

//Queue.h
//-----------------------------------------------------------
template<typename type>
class Stack
{
public:
Stack();
bool empty()const;
Error_code push(const type &);
Error_code pop();
Error_code top(type &)const;
void prin()const;
~Stack();
Stack(const Stack &);
void operator = (const Stack &);
protected:
Node<type> *top_node;
};
//------------------------------------------------------

如上我的Stack和Queue中都要调用Node.h。
但如果有个文件同时用到Stack 和 Queue时,就会出现重复定义。
如何解决呢?(不要告诉我放在一个头文件里:))
...全文
54 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
darcymei 2003-10-10
  • 打赏
  • 举报
回复
sorry!
刚才看错了
谢谢各位
darcymei 2003-10-09
  • 打赏
  • 举报
回复
比如说
现在我写一个头文件
#include "Stack.h"
#include "Queue.h"
无论我把
#ifndef _Node_H
#define _Node_H
#endif
放在Stack.h
抑或Node.h
或在新头文件里都不行
晕啊~~~~~~~~~
还请各位帮忙
plusir 2003-10-09
  • 打赏
  • 举报
回复
我想应该在Node.h中加上这个
#ifndef NODE_H
#define NODE_H
.
.
.
.
// node的定义

#endif

也不知道行不,你自己试试吧,呵呵
huigll 2003-10-09
  • 打赏
  • 举报
回复
头文件的格式是
#ifndef _STACK_H
#define _STACK_H




#endif
这样就不会重复编译。
compboy1 2003-10-09
  • 打赏
  • 举报
回复
up

70,020

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧