以下是 我的程序 ,建立文件时没有错,执行时出现难以调试的bug!而且我自己定义的显示函数也不能见内部成员数组成功输出! 错误显示请看上传图片.在这里先谢谢 大家了^_^ //SeqList.h #ifndef SEQLIST_H #define SEQLIST_H template <class Type> class SeqList { public: SeqList(Type* Array); SeqList(int MaxSize=defaultSize); ~SeqList(){delete [] data;} int Length() const{return last+1;} int Find(Type& x)const; int IsIn(Type& x); int Insert(Type& x,int i); int Remove(Type &x); int Next(Type& x); int Prior(Type& x); int IsEmpty(){return last==-1;} int IsFull(){return last==MaxSize-1;} Type Get(int i){return i<0||i>last?NULL:data[i];} void Display(); void DeleteSame(); void DeleteSmall(); void Delete(Type& x); private: Type* data; int MaxSize; int last; }; #endif //SeqList.cpp #include <iostream> #include "SeqList.h" using namespace std; #define MAX 20 template <class Type> SeqList<Type>::SeqList(Type* Array):MaxSize(sizeof(Array)/sizeof (Type)),last(MaxSize-1) { data=new Type[MaxSize]; for(int i=0;i<MaxSize;i++) data[i]=Array[i]; } template <class Type> SeqList<Type>::SeqList(int sz) { if(sz>0) { MaxSize=sz; last=-1; data=new Type[MaxSize]; } } template <class Type> int SeqList<Type>::Find(Type& x) const { int i=0; while(i<=last&&data[i]!=x) i++; if(i>last) return -1; else return i; } template <class Type> int SeqList<Type>::IsIn(Type& x) { int i=0,found=0; while(i<=last&&!found) if(dta[i]!=x) i++; else found=1; return found; } template <class Type> int SeqList<Type>::Insert(Type& x,int i) { if(i<0||i>last+1||last==MaxSize-1) return 0; else { last++; for(int j=last;j>i;j--) data[j]=data[j-1]; data[i]=x; return 1; } } template <class Type> int SeqList<Type>::Remove(Type& x) { int i=Find(x); if(i>=0) { last--; for(int j=i;j<=last;j++) data[j]=data[j+1]; data[i]=x; return 1; } else return 0; } template <class Type> int SeqList<Type>::Next(Type& x) { int i=Find(x); if(i>=0&&i<last) return i+1; else return -1; } template <class Type> int SeqList<Type>::Prior(Type& x) { int i=Find(x); if(i>0&&i<=last) return i-1; else return -1; } template <class Type> void SeqList<Type>::Display() { for(int i=0;i<MaxSize;i++) if(i%10==0) cout<<data[i]<<' '; cout<<endl; } template <class Type> void SeqList<Type>::DeleteSame() { int i,k; for(k=0;k<MAX-1;k++) for(i=k+1;i<MAX;i++) if(data[i]==data[k]) { for(int j=i;j<MAX-1;j++) data[j]=data[j+1]; MaxSize--; } } template <class Type> void SeqList<Type>::DeleteSmall() { Type temp=data[0]; int i=0; int k=0; for(i=1;i<MaxSize;i++) if(temp>data[i]) { temp=data[i]; k=i; } data[k]=data[MaxSize-1]; } template <class Type> void SeqList<Type>::Delete(Type& x) { for(int i=0;i<MaxSize;i++) if(data[i]==x) { Remove(x); MaxSize--; } } //drives.cpp #include <iostream> #include <cstdlib> #include "SeqList.h" #include "SeqList.cpp" using namespace std; #define MAX 20 int main() { int i; int Array[MAX]; for(i=0;i<MAX;i++) { Array[i]=rand()%20; cout<<Array[i]<<' '; } cout<<endl; SeqList <int> list1(Array); cout<<"Before being changed ,the list's data[] is:"<<endl; list1.Display(); int x; cout<<"Enter the 'x' you want to delete:"; cin>>x; list1.Delete(x); cout<<"After delete the same data ,the list is:"<<endl; list1.Display(); SeqList <int> list3(Array); list3.DeleteSame(); list3.Display(); cout<<"After delete the smallest data,the list is:"<<endl; SeqList <int> list2(Array); list2.DeleteSmall(); list2.Display(); return 0; }