模板类中类内声明类外定义的函数,在类外定义时没加模板时的报错

错误 1 error LNK2019: 无法解析的外部符号 "public: int __thiscall SqList<class StuTab>::getLength(void)" (?getLength@?$SqList@VStuTab@@@@QAEHXZ),该符号在函数 "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<<class StuTab>(class std::basic_ostream<char,struct std::char_traits<char> > &,class Student<class StuTab> &)" (??$?6VStuTab@@@@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@AAV?$Student@VStuTab@@@@@Z) 中被引用 H:\任老师数据结构\ConsoleApplication96\ConsoleApplication96\SeqList.obj
错误 5 error LNK2019: 无法解析的外部符号 "public: class SqList<class StuTab> & __thiscall SqList<class StuTab>::operator=(class SqList<class StuTab> const &)" (??4?$SqList@VStuTab@@@@QAEAAV0@ABV0@@Z),该符号在函数 "public: class Student<class StuTab> & __thiscall Student<class StuTab>::operator=(class Student<class StuTab> const &)" (??4?$Student@VStuTab@@@@QAEAAV0@ABV0@@Z) 中被引用 H:\任老师数据结构\ConsoleApplication96\ConsoleApplication96\SeqList.obj
错误 4 error LNK2019: 无法解析的外部符号 "public: bool __thiscall SqList<class StuTab>::NextElem(class StuTab,class StuTab &)" (?NextElem@?$SqList@VStuTab@@@@QAE_NVStuTab@@AAV2@@Z),该符号在函数 "void __cdecl ex3_2_6<class StuTab>(class Student<class StuTab> &,char &)" (??$ex3_2_6@VStuTab@@@@YAXAAV?$Student@VStuTab@@@@AAD@Z) 中被引用 H:\任老师数据结构\ConsoleApplication96\ConsoleApplication96\SeqList.obj
错误 3 error LNK2019: 无法解析的外部符号 "public: bool __thiscall SqList<class StuTab>::IsEmpty(void)" (?IsEmpty@?$SqList@VStuTab@@@@QAE_NXZ),该符号在函数 "void __cdecl ex3_2_2<class StuTab>(class Student<class StuTab> &,char &)" (??$ex3_2_2@VStuTab@@@@YAXAAV?$Student@VStuTab@@@@AAD@Z) 中被引用 H:\任老师数据结构\ConsoleApplication96\ConsoleApplication96\SeqList.obj
错误 2 error LNK2019: 无法解析的外部符号 "public: bool __thiscall SqList<class StuTab>::insert(int,class StuTab)" (?insert@?$SqList@VStuTab@@@@QAE_NHVStuTab@@@Z),该符号在函数 "void __cdecl ex3_2_12<class StuTab>(class Student<class StuTab> &,char &)" (??$ex3_2_12@VStuTab@@@@YAXAAV?$Student@VStuTab@@@@AAD@Z) 中被引用 H:\任老师数据结构\ConsoleApplication96\ConsoleApplication96\SeqList.obj

最后找出来的原因是:基类的函数成员在类外定义时没有加模板,例如:

 1 /*返回给定元素的后继元素,将后继元素存储在next_e中,返回是否返回成功*/
 2 //template<typename ElementType>    //没有加模板就会报上述错误
 3 Status SqList<ElementType>::NextElem(ElementType e, ElementType& next_e)
 4 {
 5     int i = BinSearch(e);    //获取e的序号
 6     if (i < 1 || i >= Last)        //如果待查元素不存在或者是最后一个元素都五后继
 7         return ERROR;
 8 
 9     else
10         getElem(i + 1, next_e);        //获取第i + 1个元素并存入next_e中
11     //next_e = elem[i];
12     return OK;
13 }

猜你喜欢

转载自www.cnblogs.com/hi3254014978/p/9925361.html