请高手帮帮忙C++函数调用问题

来源:百度知道 编辑:UC知道 时间:2024/06/20 13:20:45
下面的程序如果我想求顺序表的交并集,要在主函数里怎么调用啊?
#include<iostream.h>
#include<stdlib.h>
const int Defaultsize=100;
template<class Type> class SeqList{
public :
SeqList(int MaxSize=ABC);//构造函数
~ SeqList(){delete [] data;}//释放函数
int length() const{return last+1;};//计算长度
int Find (Type &x) const;//找x在表中的位置
int Insert (Type &x,int i);//在i处插入x
int Remove (Type &x);//删除x
Type Get(int i){return i<0||i>last?NULL:data[i];};//取第i个元素的值
private:
Type *data;//顺序表的存放数组
int MaxSize;//顺序表的最大可以容纳的数
int last;//顺序表当前已存表项的最后位置
};
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;
}<

注意你提供的代码有个别错误:
1、SeqList构造函数中默认参数的值ABC没有定义
2、SeqList构造函数的实现中data=new Type[Maxsize];};这句的Maxsize写错了
3、SeqList类的成员函数length()应该写成Lenght(),不然编译不过

int main()
{
SeqList<int> list1;
int a[] = {1,2,3,4};
list1.Insert(a[0], 0);
list1.Insert(a[1], 0);
list1.Insert(a[2], 0);
list1.Insert(a[3], 0);

SeqList<int> list2;
int b[] = {3,4,5,6};
list2.Insert(b[0], 0);
list2.Insert(b[1], 0);
list2.Insert(b[2], 0);
list2.Insert(b[3], 0);

Union<int>(list1, list2); // 并集
for (int i = 0; i < list1.Length(); ++i)
{
cout << list1.Get(i) << " ";
}
cout << endl;

Inter<int>(list1, list2); // 交集
for (int i = 0; i < list1.Length(); ++i)
{
cout << list1.Get(i) << " ";
}