1. 程式人生 > >模板類中類內宣告類外定義的函式,在類外定義時沒加模板時的報錯

模板類中類內宣告類外定義的函式,在類外定義時沒加模板時的報錯

錯誤 1 error LNK2019: 無法解析的外部符號 "public: int __thiscall SqList<class StuTab>::getLength(void)" ([email protected][email protected]@@@@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> &)" (

[email protected]@@@[email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected]@@@@@Z) 中被引用 H:\任老師資料結構\ConsoleApplication96\ConsoleApplication96\SeqList.obj
錯誤 5 error LNK2019: 無法解析的外部符號 "public: class SqList<class StuTab> & __thiscall SqList<class StuTab>::operator=(class SqList<class StuTab> const &)" (
[email protected]
@@@@[email protected]@@Z),該符號在函式 "public: class Student<class StuTab> & __thiscall Student<class StuTab>::operator=(class Student<class StuTab> const &)" ([email protected]@@@@[email protected]@@Z) 中被引用 H:\任老師資料結構\ConsoleApplication96\ConsoleApplication96\SeqList.obj

錯誤 4 error LNK2019: 無法解析的外部符號 "public: bool __thiscall SqList<class StuTab>::NextElem(class StuTab,class StuTab &)" ([email protected][email protected]@@@@[email protected]@[email protected]@Z),該符號在函式 "void __cdecl ex3_2_6<class StuTab>(class Student<class StuTab> &,char &)" ([email protected]@@@@[email protected]@@@@[email protected]) 中被引用 H:\任老師資料結構\ConsoleApplication96\ConsoleApplication96\SeqList.obj
錯誤 3 error LNK2019: 無法解析的外部符號 "public: bool __thiscall SqList<class StuTab>::IsEmpty(void)" ([email protected]?$SqL[email protected]@@@@QAE_NXZ),該符號在函式 "void __cdecl ex3_2_2<class StuTab>(class Student<class StuTab> &,char &)" ([email protected]@@@@[email protected]@@@@[email protected]) 中被引用 H:\任老師資料結構\ConsoleApplication96\ConsoleApplication96\SeqList.obj
錯誤 2 error LNK2019: 無法解析的外部符號 "public: bool __thiscall SqList<class StuTab>::insert(int,class StuTab)" ([email protected][email protected]@@@@[email protected]@@Z),該符號在函式 "void __cdecl ex3_2_12<class StuTab>(class Student<class StuTab> &,char &)" ([email protected]@@@@[email protected]@@@@[email protected]) 中被引用 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 }