中国对比特币最新消息:C++中的问题,急..

来源:百度文库 编辑:高校问答 时间:2024/04/30 12:17:35
我是个初学者,我的程序是这样的:
#include<iostream.h>
#include<fstream.h>
template<class T> class Queue;
template<class T>
class Node {
friend Queue<T>;
private:
T data;
Node<T> *next;
};

template<class T>
class Queue{
public:
Queue(){front=rear=0;}
~Queue();
bool Empty()const
{return((front)?false:true);}
Queue<T> &EnQueue(const T&x);
Queue<T> &DeQueue(T&x);
private:
Node<T> *front;
Node<T> *rear;
};
template<class T>
Queue<T>::~Queue()
{
Node<T> *next;
while(front){
next=front->next;
delete front;
front=next;
}
}
template<class T>
Queue<T>&Queue<T>::EnQueue(const T&x)
{
Node<T> *p=new Node<T>;
p->data=x;
p->next=0;
if(front)rear->next=p;
else front=p;
rear=p;
return *this;
}
template<class T>
Queue<T>&Queue<T>::DeQueue(T&x)
{
if(Empty())throw OutOfbounds();
x=front->data;
Node<T> *p=front;
front=front->next;
delete p;
return *this;
}
main()
{
ifstream in("input.txt");
ofstream out("output.txt");
int m,n,o,p,q,x,*lc,*rc,i;
lc=new int[m+1];
rc=new int[m+1];
in>>m;
for(i=1;i<=m;i++)
{
in>>o>>p>>q;
lc[o]=p;
rc[o]=q;
}
EnQueue(o);
while(!Empty())
{
DeQueue(x);
EnQueue(lc[o]);
EnQueue(rc[o]);
if(x!=0){out<<x<<' ';}
}
return 0;
}
写出树层序输出.
input.txt output.txt
5 1 4 2 3 5
1 4 2
4 3 0
2 5 0
3 0 0
5 0 0
调试时写着"C:\Documents and Settings\lenovo\桌面\新建文件夹 (2)\223.cpp(70) : error C2065: 'EnQueue' : undeclared identifier
C:\Documents and Settings\lenovo\桌面\新建文件夹 (2)\223.cpp(71) : error C2065: 'Empty' : undeclared identifier
C:\Documents and Settings\lenovo\桌面\新建文件夹 (2)\223.cpp(73) : error C2065: 'DeQueue' : undeclared identifier
Error executing cl.exe.

223.exe - 3 error(s), 0 warning(s)"
应如何修改?

最严重的问题是没有 对类模板进行实例化
另外OutOfbounds()不知道是做什么的,没有定义,所以我给你注释掉了
以下是可以运行的代码:
我给你实例化为int类型了

#include<iostream.h>
#include<fstream.h>
template<class T> class Queue;
template<class T>
class Node {
friend Queue<T>;
private:
T data;
Node<T> *next;
};

template<class T>
class Queue{
public:
Queue(){front=rear=0;}
~Queue();
bool Empty()const
{return((front)?false:true);}
Queue<T> &EnQueue(const T&x);
Queue<T> &DeQueue(T&x);
private:
Node<T> *front;
Node<T> *rear;
};
template<class T>
Queue<T>::~Queue()
{
Node<T> *next;
while(front){
next=front->next;
delete front;
front=next;
}
}
template<class T>
Queue<T>&Queue<T>::EnQueue(const T&x)
{
Node<T> *p=new Node<T>;
p->data=x;
p->next=0;
if(front)rear->next=p;
else front=p;
rear=p;
return *this;
}
template<class T>
Queue<T>&Queue<T>::DeQueue(T&x)
{
if(Empty())throw ;//OutOfbounds();
x=front->data;
Node<T> *p=front;
front=front->next;
delete p;
return *this;
}
main()
{
ifstream in("input.txt");
ofstream out("output.txt");
int m,n,o,p,q,x,*lc,*rc,i;
lc=new int[m+1];
rc=new int[m+1];
in>>m;
for(i=1;i<=m;i++)
{
in>>o>>p>>q;
lc[o]=p;
rc[o]=q;
}
Queue<int> test;
test.EnQueue(o);
while(!test.Empty())
{
test.DeQueue(x);
test.EnQueue(lc[o]);
test.EnQueue(rc[o]);
if(x!=0){out<<x<<' ';}
}
return 0;
}