template<class T> class Chain<T>
template<class T>
class ChainNode{ //节点类
friend class Chain<T>;
private:
T data;
ChainNode<T> *link;
};
template<class T>
class Chain{
public:
Chain(){first=0;}
~Chain();
bool IsEmpty(){return first==0;}
int length()const;
void InsertHead(const T&x);
Chain<T>&Delete(int k,T&x);
int Search(const T&x)const;
void Output()const;
private:
ChainNode<T> *first;
};
template<class T>
Chain<T>::~Chain(){
ChainNode<T> *next;
while(first){
next=first->link;
delete first;
first=next;}
}
template<class T>
int Chain<T>::length()const{
int n=0;
ChainNode<T> *current=first;
while(current){
current=current->link;
n++;
}
return n;
}
template<class T>
void Chain<T>::InsertHead(const T&x){
ChainNode<T> *first1=new ChainNode<T>;
first1->data=x;
first1->link=first;
first=first1;
}
template<class T>
Chain<T>&Chain<T>::Delete(int k,const T&x){
ChainNode<T> *current=first;
ChainNode<T> *previous=0;
for(int i=0;i<k;i++){
previous=current;
current=current->link;
}
x=current->data;
previous->link=current->link;
delete current;
return *this;
}
template<class T>
int Chain<T>::Search(const T&x)const{
ChainNode<T> *current=first;
int n1;
while(current->data!=x||current){
current=current->link;
n1++;
}
if(current){return n1;}
else return 0;//not find
}
template<class T>
void Chain<T>::Output()const{
ChainNode<T> *current=first;
while(current){cout<<current->data<<" ";}
cout<<endl;
}
主程序如下
#include<iostream.h>
#include"chain.h"
void main(){
Chain<int> c();
cout<<"请输入数字";
c.InsertHead(5);
c.InsertHead(6);
c.Output();
}
编译器报错如下
Compiling...
cn.cpp
d:\工具\visurl c++\msdev98\myprojects\lianbiao\chain.h(1) : error C2143: syntax error : missing ';' before '<'
d:\工具\visurl c++\msdev98\myprojects\lianbiao\chain.h(1) : error C2143: syntax error : missing ';' before '<'
d:\工具\visurl c++\msdev98\myprojects\lianbiao\chain.h(3) : error C2143: syntax error : missing ';' before '{'
d:\工具\visurl c++\msdev98\myprojects\lianbiao\chain.h(3) : error C2447: missing function header (old-style formal list?)
d:\工具\visurl c++\msdev98\myprojects\lianbiao\chain.h(21) : error C2143: syntax error : missing ';' before '<'
d:\工具\visurl c++\msdev98\myprojects\lianbiao\chain.h(22) : see reference to class template instantiation 'Chain<T>' being compiled
d:\工具\visurl c++\msdev98\myprojects\lianbiao\chain.h(21) : error C2501: 'ChainNode' : missing storage-class or type specifiers
d:\工具\visurl c++\msdev98\myprojects\lianbiao\chain.h(22) : see reference to class template instantiation 'Chain<T>' being compiled
d:\工具\visurl c++\msdev98\myprojects\lianbiao\chain.h(21) : error C2059: syntax error : '<'
d:\工具\visurl c++\msdev98\myprojects\lianbiao\chain.h(22) : see reference to class template instantiation 'Chain<T>' being compiled
d:\工具\visurl c++\msdev98\myprojects\lianbiao\chain.h(21) : error C2238: unexpected token(s) preceding ';'
d:\工具\visurl c++\msdev98\myprojects\lianbiao\chain.h(22) : see reference to class template instantiation 'Chain<T>' being compiled
d:\工具\visurl c++\msdev98\myprojects\lianbiao\chain.h(60) : error C2244: 'Chain<T>::Delete' : unable to resolve function overload
d:\工具\visurl c++\msdev98\myprojects\lianbiao\chain.h(61) : error C2954: template definitions cannot nest
d:\工具\visurl c++\msdev98\myprojects\lianbiao\cn.cpp(6) : error C2228: left of '.InsertHead' must have class/struct/union type
d:\工具\visurl c++\msdev98\myprojects\lianbiao\cn.cpp(7) : error C2228: left of '.InsertHead' must have class/struct/union type
执行 cl.exe 时出错.
lianbiao.exe - 1 error(s), 0 warning(s)
这是怎么回事啊 请高手指点 小弟感激不尽