求大神:C++编译错误error C2143: syntax error : missing ';' before 'template'
这是我的程序,请问我应该怎么改啊?谢谢大家!#include<iostream.h>#include<stdlib.h>template<classType>classS...
这是我的程序,请问我应该怎么改啊?谢谢大家!
#include<iostream.h>
#include<stdlib.h>
template <class Type>
class Seqlist {
private:
Type *data; //顺序表存储数组
int MaxSize; //最大允许长度
int last; //当前最后元素下标
public:
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];
}
}
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
{
//搜索函数:在表中从前向后顺序查找 x
int i = 0;
while ( i <= last && data[i] != x )
i++;
if ( i > last ) return -1;
else return i;
}
template <class Type>
int Seqlist<Type>::Insert ( Type & x, int i ){
//在表中第 i 个位置插入新元素 x
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 ) {
//在表中删除已有元素 x
int i = Find (x); //在表中搜索 x
if ( i >= 0 ) {
last-- ;
for ( int j=i; j<=last; j++ )
data[j] = data[j+1];
return 1; //成功删除
}
return 0; //表中没有 x
}
int main()
{
int a,b,c,d;
int m[10];
cout<<"请输入一个10个元素的数组:"<<endl;
for(int i=0;i<10;i++)
cin>>m[i];
Seqlist<int>seq1(10);
cout<<"请输入您要查找的元素"<<endl;
cin>>a;
seq1.Find(a);
cout<<"请输入您要插入的元素及位置"<<endl;
cin>>b>>c;
seq1.Insert(b,c);
cout<<"请输入您要删除的元素"<<endl;
cin>>d;
seq1.Remove(d);
return 0;
} 展开
#include<iostream.h>
#include<stdlib.h>
template <class Type>
class Seqlist {
private:
Type *data; //顺序表存储数组
int MaxSize; //最大允许长度
int last; //当前最后元素下标
public:
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];
}
}
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
{
//搜索函数:在表中从前向后顺序查找 x
int i = 0;
while ( i <= last && data[i] != x )
i++;
if ( i > last ) return -1;
else return i;
}
template <class Type>
int Seqlist<Type>::Insert ( Type & x, int i ){
//在表中第 i 个位置插入新元素 x
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 ) {
//在表中删除已有元素 x
int i = Find (x); //在表中搜索 x
if ( i >= 0 ) {
last-- ;
for ( int j=i; j<=last; j++ )
data[j] = data[j+1];
return 1; //成功删除
}
return 0; //表中没有 x
}
int main()
{
int a,b,c,d;
int m[10];
cout<<"请输入一个10个元素的数组:"<<endl;
for(int i=0;i<10;i++)
cin>>m[i];
Seqlist<int>seq1(10);
cout<<"请输入您要查找的元素"<<endl;
cin>>a;
seq1.Find(a);
cout<<"请输入您要插入的元素及位置"<<endl;
cin>>b>>c;
seq1.Insert(b,c);
cout<<"请输入您要删除的元素"<<endl;
cin>>d;
seq1.Remove(d);
return 0;
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询