|
|
#2
寒风中的细雨2010-12-05 23:01
上面的vC6中也不行 与标准有冲突
#include<iostream.h> //using namespace std; template<class T> class AbstractList { public: virtual bool isEmpty() const=0; virtual int length() const=0; virtual bool find(int k,T&x)const=0; virtual int search(const T&x)const=0; virtual AbstractList<T>& Delete(int k,T&x)=0; virtual AbstractList<T>& insert(int k, const T&x)=0; virtual void output(ostream &out)const=0; };//是下面LinearList:public AbstractList<T>作为基类的 class OutOfBounds { public: OutOfBounds(){} ~OutOfBounds(){} const char *ShowReason() const { return "下标越界"; } }; class NoMen { public: NoMen(){} ~NoMen(){} const char *ShowReason() const { return "存储溢出"; } };//老师添得作为程序的完整 template<class T> class LinearList:public AbstractList<T> { public: LinearList(int maxListSize=10); ~LinearList(){delete[]element;} bool isEmpty()const{return len==0;} int length()const{return len;} bool find(int k,T&x)const; int search(const T&x)const; AbstractList<T>&Delete(int k,T&x); AbstractList<T>&insert(int k, const T&x); void output(ostream &out)const;//把线性表放入输出流out中 friend ostream &operator<<(ostream& out, LinearList<T> &L);//重载运算符<< private: int len; int maxSize; T *element; };//LinearList类的定义,里面的成员函数均是申明其具体定义在下面 template<class T>//作为模板都要写的,且一句只作用一个函数 LinearList<T>::LinearList(int maxListSize) { maxSize=maxListSize; element=new T[maxSize]; len=0; }//LinearList 构造函数的定义 template<class T> bool LinearList<T>::find(int k,T&x)const//find(int k,T&x)寻找表中第K个元素,并把它保存到x中;如果不存在则返回false { if (k<1||k>len) return false; x=element[k-1]; return true; } template<class T> int LinearList<T>::search(const T&x)const { for(int i=0;i<len;i++) if (element[i]==x) return 1; return 0; } template<class T> AbstractList<T>&LinearList<T>::Delete(int k,T&x)//删除表中第k个元素,保存到x,返回修改后的线性表 { if(find(k,x)) { for(int i=k;i<len;i++) element[i-1]=element[i]; len--; return *this; } else throw OutOfBounds(); } template<class T> AbstractList<T>&LinearList<T>::insert(int k,const T&x)//在第k个元素之后插入X,返回修改后的线性表 { if(k<0||k>len) throw OutOfBounds(); if(len==maxSize) throw NoMen(); for(int i=len-1;i>k;i--) element[i+1]=element[i]; element[k]=x; len++; return *this; } template<class T> void LinearList<T>::output(ostream &out)const//把线性表放入输出流out中 { for (int i=0; i<length() ; i++) out<<element[i]<<' '; out<<endl; } template<class T> ostream &operator<<(ostream& out, LinearList<T> &L)//重载运算符<< { for (int i=0; i<L.length() ; i++) out<<L.element[i]<<' '; out<<endl; return out; } void main()//主函数 { LinearList<int> L(5); //建立空的顺序表 cout<<"Length="<<L.length()<<endl; cout<<"isEmpty="<<L.isEmpty()<<endl; L.insert(0,2).insert(1,6); cout<<"List is "<<L<<endl; cout<<"isEmpty="<<L.isEmpty()<<endl; L.insert(0,22).insert(3,666); cout<<"List is "<<L<<endl; //用cout输出 int z; L.find(1,z); cout<<"first element is "<<z<<endl; L.Delete(1,z); cout<<"Deleted element is "<<z<<endl; cout<<"List is "<<L<<endl; if ((z=L.search(6))>0) cout<<"Search in location "<<z<<endl; else cout<<"No such data."<<endl; L.output(cout); //用Output输出 } |
#include<iostream>
using namespace std;
template<class T>
class AbstractList
{
public:
virtual bool isEmpty() const=0;
virtual int length() const=0;
virtual bool find(int k,T&x)const=0;
virtual int search(const T&x)const=0;
virtual AbstractList<T>& Delete(int k,T&x)=0;
virtual AbstractList<T>&insert(int k, const T&x)=0;
virtual void output(ostream &out)const=0;
};//是下面LinearList:public AbstractList<T>作为基类的
class OutOfBounds {
public:
OutOfBounds(){}
~OutOfBounds(){}
const char *ShowReason() const {
return "下标越界";
}
};
class NoMen{
public:
NoMen(){}
~NoMen(){}
const char *ShowReason() const {
return "存储溢出";
}
};//老师添得作为程序的完整
template<class T>
class LinearList:public AbstractList<T>
{
public:
LinearList(int maxListSize=10);
~LinearList(){delete[]element;}
bool isEmpty()const{return len==0;}
int length()const{return len;}
bool find(int k,T&x)const;
int search(const T&x)const;
AbstractList<T>&Delete(int k,T&x);
AbstractList<T>&insert(int k, const T&x);
void output(ostream &out)const;//把线性表放入输出流out中
friend ostream &operator<<(ostream& out, LinearList &L);//重载运算符<<
private:
int len;
int maxSize;
T *element;
};//LinearList类的定义,里面的成员函数均是申明其具体定义在下面
template<class T>//作为模板都要写的,且一句只作用一个函数
LinearList<T>::LinearList(int maxListSize)
{
maxSize=maxListSize;
element=new T[maxSize];
len=0;
}//LinearList 构造函数的定义
template<class T>
bool LinearList<T>::find(int k,T&x)const//find(int k,T&x)寻找表中第K个元素,并把它保存到x中;如果不存在则返回false
调试的结果为
1>------ 已启动生成: 项目: 0123, 配置: Debug Win32 ------
1>生成启动时间为 2010/12/4 21:48:54。
1>InitializeBuildStatus:
1> 正在对“Debug\0123.unsuccessfulbuild”执行 Touch 任务。
1>ClCompile:
1> 所有输出均为最新。
1>ManifestResourceCompile:
1> 所有输出均为最新。
1>main.obj : error LNK2019: 无法解析的外部符号 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class LinearList<int> &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$LinearList@H@@@Z),该符号在函数 _main 中被引用
1>c:\users\徐施展\0123\Debug\0123.exe : fatal error LNK1120: 1 个无法解析的外部命令
1>
1>生成失败。
1>
1>已用时间 00:00:00.73
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
{
if (k<1||k>len)return false;
x=element[k-1];
return true;}
template<class T>
int LinearList<T>::search(const T&x)const{
for(int i=0;i<len;i++)
if (element[i]==x)return 1;
return 0;
}
template<class T>
AbstractList<T>&LinearList<T>::Delete(int k,T&x)//删除表中第k个元素,保存到x,返回修改后的线性表
{
if(find(k,x))
{for(int i=k;i<len;i++)
element[i-1]=element[i];
len--;
return *this;}
else throw OutOfBounds();
}
template<class T>
AbstractList<T>&LinearList<T>::insert(int k,const T&x)//在第k个元素之后插入X,返回修改后的线性表
{
if(k<0||k>len)throw OutOfBounds();
if(len==maxSize)throw NoMen();
for(int i=len-1;i>k;i--)
element[i+1]=element[i];
element[k]=x;
len++;
return *this;
}
template<class T>
void LinearList<T>::output(ostream &out)const//把线性表放入输出流out中
{
for (int i=0; i<length() ; i++)
out<<element[i]<<' ';
out<<endl;
}
template<class T>
ostream &operator<<(ostream& out, LinearList<T> &L)//重载运算符<<
{
for (int i=0; i<L.length() ; i++)
out<<L.element[i]<<' ';
out<<endl;
return out;
}
void main()//主函数
{
LinearList<int> L(5); //建立空的顺序表
cout<<"Length="<<L.length()<<endl;
cout<<"isEmpty="<<L.isEmpty()<<endl;
L.insert(0,2).insert(1,6);
cout<<"List is "<<L<<endl;
cout<<"isEmpty="<<L.isEmpty()<<endl;
L.insert(0,22).insert(3,666);
cout<<"List is "<<L<<endl; //用cout输出
int z;
L.find(1,z);
cout<<"first element is "<<z<<endl;
L.Delete(1,z);
cout<<"Deleted element is "<<z<<endl;
cout<<"List is "<<L<<endl;
if ((z=L.search(6))>0)
cout<<"Search in location "<<z<<endl;
else
cout<<"No such data."<<endl;
L.output(cout); //用Output输出
}