主题:[讨论]求助:编译老是痛不过,请高手看看
我在Win32 console application 环境下创建一个工程.
头文件是:
*******************************************************
chain.h
*****************************************************
#ifndef ChainNode_
#define ChainNode_
#include<iostream.h>
template <class T>
class ChainNode {
friend Chain<T>;
private:
T data;
ChainNode<T> *link;
};
template <class T>
class Chain {
public:
Chain() {first=0;}
~Chain();
bool IsEmpty() const {return first ==0;}
int Length() const;
bool Find(int k,T& x) const;
int Search(const T& x) const;
Chain<T>& Delete(int k,T& x);
Chain<T>& Insert(int k,const T& X);
void Output(ostream& out) const;
private:
ChainNode<T> *first; //pointer to first node.
};
#endif
源文件是:
*****************************************************
chain.cpp
*********************************************************
#include "chain.h"
template <class T>
Chain<T>::~Chain()
{//Chain destructor.Delete all nodes in chain.
ChainNode<T> *next; //next node
while (first) {
next=first->link;
delete first;
first=next;
}
}
template <class T>
int Chain<T>::Length() const
{//Return the number of elements in the chain.
ChainNode<T>* current =first;
int len =0;
while (current) {
len++;
current=current->link;
}
return len;
}
template <class T>
bool Chain<T>::Find(int k,T& X) const
{//Set x to the k'th element in the chain.
//Return false if no k'th ;return true otherwise.
if (k<1) return false;
ChainNode<T> *current=first;
int index=1; //index of current
while (index<k && current){
current=current->link;
index++;
}
if(current) {x=current->data;
return true;}
return false ;//no k'th element
}
template<class T>
int Chain<T>::Search(const T& x) const
{//Locate x. Return position of x if found.
//Return 0 if x not int the chain.
ChainNode<T> *current=first;
int index=1; //index of current
while (current && current->data!=x) {
current = current->link;
index++;
}
if (current) return index;
return 0;
}
template<class T>
void Chain<T>::Output(ostream& out) const
{//Insert the chain elements into the stream out.
ChainNode<T> * current;
for (current=first; current; current=current->link)
out<<current->data<<" ";
}
//overload<<
template <class T>
ostream& operator<<(ostream& out ,const Chain<T>& X)
{X.Output(out); return out;}
template<class T>
Chain<T>& Chain<T>::Delete(int k,T& x)
{//Set x to the k'th element and delete it.
//Throw OutOfBounds exception if no k'th element.
if(k<1 || !first)
throw OutOfBounds(); //no k'th
//p will eventually point to k'th node
ChainNode<T> *p=first;
//move p to k'th & remove from chain
ChainNode<T> *P=first;
//move p to k'th & remove from chain
if (k==1) //p already at k'th
first=first->link; //remove
else{ //use q to get to k-1'st
ChainNode<T> *q =first;
for (int index=1;index<k-1 && q;index++)
q=q->link;
if(!q || !q->link)
throw OutOfBounds(); //no k'th
p=q->link; //k'th
q->link=p->link;} //remove from chain
//save k'th element and free node p
x=p->data;
delete p;
return *this;
}
template<class T>
Chain<T>& Chain<T>::Insert(int k,const T& X)
{//Insert x after the k'th element .
//Throw OutOfBounds exception if no k'th element.
//Pass NoMem exception if inadequate space.
if(k<0)throw OutOfBounds();
//p will eventually point to k'th node
ChainNode<T> *p=first;
for (int index=1;index<k && p;index++) //move to k'th
p=p->link;
if (k>0 && !p) throw OutOfBounds(); //no k'th
//insert
ChainNode<T> *y = new ChainNode<T>;
y->data= x;
if (k) {//insert after p
y->link=p->link;
p->link=y;}
else{//insert as first element
y->link=first;
first=y;}
return *this;
}
int main()
{
teturn 0;
}
*****************************************
这是一个实现链表的源代码,
可是总是提示出错:
chain.cpp
d:\课件\数据结构练习\链表\chain.h(24) : error C2143: syntax error : missing ';' before '<'
d:\课件\数据结构练习\链表\chain.h(25) : see reference to class template instantiation 'Chain<T>' being compiled
d:\课件\数据结构练习\链表\chain.h(24) : error C2501: 'ChainNode' : missing storage-class or type specifiers
d:\课件\数据结构练习\链表\chain.h(25) : see reference to class template instantiation 'Chain<T>' being compiled
d:\课件\数据结构练习\链表\chain.h(24) : error C2059: syntax error : '<'
d:\课件\数据结构练习\链表\chain.h(25) : see reference to class template instantiation 'Chain<T>' being compiled
d:\课件\数据结构练习\链表\chain.h(24) : error C2238: unexpected token(s) preceding ';'
d:\课件\数据结构练习\链表\chain.h(25) : see reference to class template instantiation 'Chain<T>' being compiled
执行 cl.exe 时出错.
链表.exe - 1 error(s), 0 warning(s)
头文件是:
*******************************************************
chain.h
*****************************************************
#ifndef ChainNode_
#define ChainNode_
#include<iostream.h>
template <class T>
class ChainNode {
friend Chain<T>;
private:
T data;
ChainNode<T> *link;
};
template <class T>
class Chain {
public:
Chain() {first=0;}
~Chain();
bool IsEmpty() const {return first ==0;}
int Length() const;
bool Find(int k,T& x) const;
int Search(const T& x) const;
Chain<T>& Delete(int k,T& x);
Chain<T>& Insert(int k,const T& X);
void Output(ostream& out) const;
private:
ChainNode<T> *first; //pointer to first node.
};
#endif
源文件是:
*****************************************************
chain.cpp
*********************************************************
#include "chain.h"
template <class T>
Chain<T>::~Chain()
{//Chain destructor.Delete all nodes in chain.
ChainNode<T> *next; //next node
while (first) {
next=first->link;
delete first;
first=next;
}
}
template <class T>
int Chain<T>::Length() const
{//Return the number of elements in the chain.
ChainNode<T>* current =first;
int len =0;
while (current) {
len++;
current=current->link;
}
return len;
}
template <class T>
bool Chain<T>::Find(int k,T& X) const
{//Set x to the k'th element in the chain.
//Return false if no k'th ;return true otherwise.
if (k<1) return false;
ChainNode<T> *current=first;
int index=1; //index of current
while (index<k && current){
current=current->link;
index++;
}
if(current) {x=current->data;
return true;}
return false ;//no k'th element
}
template<class T>
int Chain<T>::Search(const T& x) const
{//Locate x. Return position of x if found.
//Return 0 if x not int the chain.
ChainNode<T> *current=first;
int index=1; //index of current
while (current && current->data!=x) {
current = current->link;
index++;
}
if (current) return index;
return 0;
}
template<class T>
void Chain<T>::Output(ostream& out) const
{//Insert the chain elements into the stream out.
ChainNode<T> * current;
for (current=first; current; current=current->link)
out<<current->data<<" ";
}
//overload<<
template <class T>
ostream& operator<<(ostream& out ,const Chain<T>& X)
{X.Output(out); return out;}
template<class T>
Chain<T>& Chain<T>::Delete(int k,T& x)
{//Set x to the k'th element and delete it.
//Throw OutOfBounds exception if no k'th element.
if(k<1 || !first)
throw OutOfBounds(); //no k'th
//p will eventually point to k'th node
ChainNode<T> *p=first;
//move p to k'th & remove from chain
ChainNode<T> *P=first;
//move p to k'th & remove from chain
if (k==1) //p already at k'th
first=first->link; //remove
else{ //use q to get to k-1'st
ChainNode<T> *q =first;
for (int index=1;index<k-1 && q;index++)
q=q->link;
if(!q || !q->link)
throw OutOfBounds(); //no k'th
p=q->link; //k'th
q->link=p->link;} //remove from chain
//save k'th element and free node p
x=p->data;
delete p;
return *this;
}
template<class T>
Chain<T>& Chain<T>::Insert(int k,const T& X)
{//Insert x after the k'th element .
//Throw OutOfBounds exception if no k'th element.
//Pass NoMem exception if inadequate space.
if(k<0)throw OutOfBounds();
//p will eventually point to k'th node
ChainNode<T> *p=first;
for (int index=1;index<k && p;index++) //move to k'th
p=p->link;
if (k>0 && !p) throw OutOfBounds(); //no k'th
//insert
ChainNode<T> *y = new ChainNode<T>;
y->data= x;
if (k) {//insert after p
y->link=p->link;
p->link=y;}
else{//insert as first element
y->link=first;
first=y;}
return *this;
}
int main()
{
teturn 0;
}
*****************************************
这是一个实现链表的源代码,
可是总是提示出错:
chain.cpp
d:\课件\数据结构练习\链表\chain.h(24) : error C2143: syntax error : missing ';' before '<'
d:\课件\数据结构练习\链表\chain.h(25) : see reference to class template instantiation 'Chain<T>' being compiled
d:\课件\数据结构练习\链表\chain.h(24) : error C2501: 'ChainNode' : missing storage-class or type specifiers
d:\课件\数据结构练习\链表\chain.h(25) : see reference to class template instantiation 'Chain<T>' being compiled
d:\课件\数据结构练习\链表\chain.h(24) : error C2059: syntax error : '<'
d:\课件\数据结构练习\链表\chain.h(25) : see reference to class template instantiation 'Chain<T>' being compiled
d:\课件\数据结构练习\链表\chain.h(24) : error C2238: unexpected token(s) preceding ';'
d:\课件\数据结构练习\链表\chain.h(25) : see reference to class template instantiation 'Chain<T>' being compiled
执行 cl.exe 时出错.
链表.exe - 1 error(s), 0 warning(s)