zoukankan      html  css  js  c++  java
  • 模板类中类内声明类外定义的函数,在类外定义时没加模板时的报错

    错误 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:任老师数据结构ConsoleApplication96ConsoleApplication96SeqList.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:任老师数据结构ConsoleApplication96ConsoleApplication96SeqList.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:任老师数据结构ConsoleApplication96ConsoleApplication96SeqList.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:任老师数据结构ConsoleApplication96ConsoleApplication96SeqList.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:任老师数据结构ConsoleApplication96ConsoleApplication96SeqList.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 }

     PS:以后再遇到类似根据编译器提示不明确的问题时,可以考虑换一个编译器重新编译试试,因为每个编译器的报错信息都不一样,我们很可能通过新的报错提示找到错误的原因,这次我就是通过吧程序复制到codeblocks上重新编译才发现的错误所在,codeblocks的其中一个错误提示是:

    E:C++common5555myhead.h|32|warning: extra tokens at end of #endif directive [-Wendif-labels]|
    E:C++common5555SqList.h|133|error: specializing member 'SqList<int>::getLength' requires 'template<>' syntax|
    E:C++common5555SqList.h|139|error: specializing member 'SqList<int>::getListSize' requires 'template<>' syntax|
    E:C++common5555SqList.h|145|error: specializing member 'SqList<int>::insert' requires 'template<>' syntax|
    可以很容看出,错误是少了模板

  • 相关阅读:
    Java日期与时间的处理/Date,String,Calendar转换
    swift中的&---文章过时重置
    函数
    分支语句?
    NSDateFormatter 'YYYY' 和 'yyyy' 的区别
    swift字典集合---文章过时重置
    Swift字符串的插入、删除和替换-备
    PHP 时间函数集合
    PHP 正则通配符
    PHP的数据库 之 关闭问题
  • 原文地址:https://www.cnblogs.com/hi3254014978/p/9925361.html
Copyright © 2011-2022 走看看