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|
    可以很容看出,错误是少了模板

  • 相关阅读:
    STM32 Timer : Base Timer, Input Capture, PWM, Output Compare
    Bus Blaster v4 design overview
    JTAG Communications model
    ARM JTAG 信号 RTCK 应该如何处理?
    HappyJTAG2
    用SWD调试接口测量代码运行时间 ( SWO )
    ARM Cortex Design Considerations for Debug
    Technical Information ARM-related JTAG / SWD / SWV / ETM Target Interfaces
    Keil debugging techniques and alternative printf (SWO function)
    Serial Wire Viewer (SWV)
  • 原文地址:https://www.cnblogs.com/hi3254014978/p/9925361.html
Copyright © 2011-2022 走看看