| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 677 人关注过本帖
标题:[求助][经验]
取消只看楼主 加入收藏
nick_annie
Rank: 1
等 级:新手上路
帖 子:105
专家分:0
注 册:2005-11-19
收藏
 问题点数:0 回复次数:1 
[求助][经验]


#include<iostream.h>
enum resultcode{Underflow,OutOfBounds};
template<class T>
struct Node
{
Node(){link=NULL;}
Node(T e,Node* next)
{
element=e;
link=next;
}
T element;
Node* link;
};
template<class T>
class LinkList
{
public:
LinkList()
{
first=NULL;
n=0;
}
~LinkList()
{
n=0;
}
void Insert(int pos,const T& x);
void Remove(int pos);
void Retrieve(int pos,T& x)const;
void Replace(int pos,const T& x);
int length()const{return n;}
bool IsEmpty()const{return first==NULL;}
protected:
Node<T> *SetPos(int pos) const;
void Output(ostream& out)const;
Node<T> *first;
int n;
friend ostream& operator<<(ostream& out,const LinkList<T>& l);
};

template<class T>
Node<T>* LinkList<T>::SetPos(int pos)const
{
Node<T>* q=first;
for(int i=0;i<pos;i++)
q=q->link;
return q;
}

template<class T>
void LinkList<T>::Insert(int pos,const T& x)
{
Node<T> *q,*p;
if(pos<0||pos>n)
throw OutOfBounds;
p=new Node<T>(x,NULL);
if(pos>0)
{
q=SetPos(pos-1);
p->link=q->link;
q->link=p;
}
else
{
p->link=first;
first=p;
}
n++;
}

template<class T>
void LinkList<T>::Remove(int pos)
{
Node<T>* q,*p;
if(IsEmpty())
throw Underflow;
if(pos<0||pos>n)
throw OutOfBounds;
if(pos==0)
{
p=first;
first=first->link;
}
else
{
q=SetPos(pos-1);
p=q->link;
q->link=p->link;
}
delete p;
n--;
}

template<class T>
void LinkList<T>::Retrieve(int pos,T& x)const
{
if(IsEmpty())
throw Underflow;
if(pos<0||pos>n)
throw OutOfBounds;
Node<T>* q=SetPos(pos);
x=q->element;
}

template<class T>
void LinkList<T>::Replace(int pos,const T& x)
{
if(IsEmpty())
throw Underflow;
if(pos<0||pos>n)
throw OutOfBounds;
Node<T>* q=SetPos(pos);
q->element=x;
}

template<class T>
ostream& operator<<(ostream& out,const LinkList<T>& l)
{
l.Output(out);
return out;
}

template<class T>
void LinkList<T>::Output(ostream& out)const
{
out<<"The list contains:";
for(;first;first=first->link)
out<<' '<<first->element;
out<<endl;
}

void main()
{
try{

LinkList<int> A;
cout<<"请输入8个整数:";
for(int i=0;i<8;i++)
{
int j;
cin>>j;
A.Insert(i,j);
}
cout<<"线性表为:"<<A;
A.Remove(3);
cout<<A;
cout<<"用11代替第四个位置:";
A.Replace(4,11);
cout<<A;
cout<<"第五个位置的值为:";
int k=A.Retrieve(5,k);
cout<<k<<endl;
}
catch(resultcode err){
switch(err)
{
case Underflow:cout<<"underflow"<<endl;break;
case OutOfBounds:cout<<"outofbounds"<<endl;break;
}
}
}




下面是错误
--------------------Configuration: 6598 - Win32 Debug--------------------
Compiling...
3.cpp
C:\Program Files\Microsoft Visual Studio\MyProjects\6598\3.cpp(131) : error C2244: 'LinkList<T>::Output' : unable to resolve function overload
C:\Program Files\Microsoft Visual Studio\MyProjects\6598\3.cpp(142) : error C2065: 'j' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\6598\3.cpp(143) : error C2065: 'A' : undeclared identifier
C:\Program Files\Microsoft Visual Studio\MyProjects\6598\3.cpp(143) : error C2228: left of '.Insert' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\6598\3.cpp(146) : error C2228: left of '.Remove' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\6598\3.cpp(149) : error C2228: left of '.Replace' must have class/struct/union type
C:\Program Files\Microsoft Visual Studio\MyProjects\6598\3.cpp(152) : error C2228: left of '.Retrieve' must have class/struct/union type
Error executing cl.exe.

3.obj - 7 error(s), 0 warning(s)
该怎么解决呢??

搜索更多相关主题的帖子: 经验 
2006-05-01 16:42
nick_annie
Rank: 1
等 级:新手上路
帖 子:105
专家分:0
注 册:2005-11-19
收藏
得分:0 
谢了啊...犯了个低级错误..问题我已经解决了....谢谢指点

2006-05-03 12:25
快速回复:[求助][经验]
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017110 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved