请问一下为什么我的代码第一行报错呀?
#include<iostream>
using namespace std;
template<class DT>
struct SqList
{
DT* elem;//基址
int size;//表容量
int length;//表长
};//顺序表表格;
template<class DT>
bool GetElem_i(SqList<DT> L, int i, DT& e)//按位序查找
{
if (i<1 || i>L.length)//位序不合理,不能获取
{
cout << "该元素不存在!" << endl;
return false;
}
e = L.elem[i];//获取查找的元素
return true;//查找成功
}
template<class DT>
int LocateElem_e(SqList<DT>& L, DT e)//按值查找
{
for (int j = 0; j < L.length; j++)//顺序查找
if (L.elem[j] == e) return j + 1;//返回元素位序
return 0;//查找失败
}
template<class DT>
bool InsertElem(SqList<DT>& L, int i, DT e)
{
if (L.length >= L.size) return false;//表满,不能插入
if (i<1 || i>L.length) return false;//位置不合理,不能插入
for (int j = L.length; j >= i; j--)
L.elem[j] = L.elem[j - 1];
L.elem[i - 1] = e;
L.length++;
return true;
}
template<class DT>
void CreateList(SqList<DT>& L, int n)//创建集合
{
int count = 0;//初始化集合数量
DT elem;
bool exist = false;
cout << "请依次输入" << n << "个元素值:" << endl;//提示用户输入集合
for (int i = 1; i <= n; i++)//输入n个元素
{
cin >> elem;//输入元素
for (int j = 0; j < count; j++)//判断是否存在相同元素
if (L.elem[j] == elem) elem = true;
if (!exist)//如果不存在相同元素就加入L
{
L.elem[count] = elem;
count++;//数组下表后移
}
}
L.length = count;
}
template<class DT>
void Interset(SqList<DT> La, SqList<DT> Lb, SqList<DT>& Lc)
{
int j = 0;//设置Lc中首个元素的存储位置
for (int i = 1; i <= La.length; i++)//遍历La
{
DT e;
if (GetElem_i(La, i, e))//按位序获取La中的每个元素e
if (LocateElem_e(Lb, e))//如果Lb中有元素e
InsertElem(Lc, j++, e);//将e添加在表尾
}
Lc.length = j;//Lc表长为Lc中元素个数
}
template<class DT>
void Union(SqList<DT> La, SqList<DT> Lb, SqList<DT>& Lc)//并集
{
int j = 0;
DT e;
int a = La.length - 1;
for (int i = 1; i <= La.length; i++)
if (GetElem_i(La, i, e))
InsertElem(Lc, j++, e);//先将集合A中的元素都插入到C中
for (int k = 1; k <= Lb.length; k++)
{
if (GetElem_i(Lb, k, e))
if (!LocateElem_e(La, e))
InsertElem(Lc, a++, e);
}//将集合B有但集合A没有的元素插入到C中
Lc.length = a;//表长
}
template<class DT>
void SubSet(SqList<DT> La, SqList<DT> Lb, SqList<DT>& Lc)
{
DT e;
int j = 0;//设置La中首个元素的存储位置
for (int i = 1; i <= La.length; i++)//遍历La
{
if (GetElem_i(La, i, e))//按位序获得La中的每个元素e
if (!LocateElem_e(Lb, e))//如果Lb中没有元素e
InsertElem(Lc, j++, e);//将e添加在Lc表尾
}
Lc.length = j;//Lc表长为其表中元素个数
}
template<class DT>
int main()
{
int na, nb;//A,B,C三个集合分别含有的元素个数
SqList<DT> La,Lb;//集合A,B
SqList<DT> Lc, Ld, Le;//顺序表
cout << "创建集合A,B\n";
cout << "输入集合A元素个数;";
cin >> na ;
cout << "创建集合A元素:";
CreateList(La, na);//创建A
cout << "输入集合B元素个数:";
cin >> nb;
cout << "创建集合B元素:";
CreateList(Lb, nb);//创建B
cout << "A与B的交集为:";
Interset(La, Lb, Lc);//使用函数得到A与B的并集
for (int i = 0; i < Lc.length; i++) cout << Lc.elem[i] << '\t';//输出并集Lc
cout << '\n';//换行
cout << "A与B的并集:";
Union(La, Lb, Ld);
for (int i = 0; i < Ld.length; i++) cout << Ld.elem[i] << '\t';
cout << '\n';
cout << "A与B的差集:";
SubSet(La, Lb, Le);
for (int i = 0; i < Le.length; i++) cout << Le.elem[i] << '\t';
}
main 函数怎么能定义成 模板函数呢,
main函数里面用到的 DT ,需要使用具体的数据类型,不然你定义模板的意义在哪里?
//template<class DT>
int main()
{
int na, nb;//A,B,C三个集合分别含有的元素个数
SqList<int> La,Lb;//集合A,B
SqList<int> Lc, Ld, Le;//顺序表
cout << "创建集合A,B\n";
cout << "输入集合A元素个数;";
cin >> na ;
cout << "创建集合A元素:";
CreateList(La, na);//创建A
cout << "输入集合B元素个数:";
cin >> nb;
cout << "创建集合B元素:";
CreateList(Lb, nb);//创建B
cout << "A与B的交集为:";
Interset(La, Lb, Lc);//使用函数得到A与B的并集
for (int i = 0; i < Lc.length; i++) cout << Lc.elem[i] << '\t';//输出并集Lc
cout << '\n';//换行
cout << "A与B的并集:";
Union(La, Lb, Ld);
for (int i = 0; i < Ld.length; i++) cout << Ld.elem[i] << '\t';
cout << '\n';
cout << "A与B的差集:";
SubSet(La, Lb, Le);
for (int i = 0; i < Le.length; i++) cout << Le.elem[i] << '\t';
return 0;
}