c++ - 收到基于模板类及其子类函数调用的错误

标签 c++ templates visual-c++ inheritance

1>main.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::getfirst(int &)" (?getfirst@?$LinkedSortedList@H@@UAE_NAAH@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall LinkedSortedList<int>::clear(void)" (?clear@?$LinkedSortedList@H@@UAEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual void __thiscall LinkedSortedList<int>::print(void)const " (?print@?$LinkedSortedList@H@@UBEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::insert(int)" (?insert@?$LinkedSortedList@H@@UAE_NH@Z) referenced in function _main
1>main.obj : error LNK2001: unresolved external symbol "public: virtual bool __thiscall LinkedSortedList<int>::find(int)const " (?find@?$LinkedSortedList@H@@UBE_NH@Z)
1>main.obj : error LNK2001: unresolved external symbol "public: virtual int __thiscall LinkedSortedList<int>::size(void)const " (?size@?$LinkedSortedList@H@@UBEHXZ)
1>c:\users\chris\documents\visual studio 2010\Projects\lab0\Debug\lab0.exe : fatal error LNK1120: 6 unresolved externals

这是我在尝试编译代码时收到的结果。我已将其缩小到(我相信)此处的这部分代码:

#ifndef _LinkedSortedListClass_
#define _LinkedSortedListClass_


    #include "LinkedNode.h"
    #include "SortedList.h"

    template <class Elm>
    class LinkedSortedList: public SortedList<int> {    
    public:

        void clear();

        bool insert(Elm newvalue);

        bool getfirst(Elm &returnvalue);

        void print() const;

        bool find(Elm searchvalue) const;

        int size() const;

    private:
            LinkedNode<Elm>* head;
    };

    #endif

这是SortedList的子类,就是这个,以备不时之需..

#ifndef _SortedListClass_
#define _SortedListClass_

template <class Elm> class SortedList {
public:

  // -------------------------------------------------------------------
  // Pure virtual functions -- you must implement each of the following
  // functions in your implementation:
  // -------------------------------------------------------------------

  // Clear the list.  Free any dynamic storage.
  virtual void clear() = 0;          

  // Insert a value into the list.  Return true if successful, false
  // if failure.
  virtual bool insert(Elm newvalue) = 0;

  // Get AND DELETE the first element of the list, placing it into the
  // return variable "value".  If the list is empty, return false, otherwise
  // return true.
  virtual bool getfirst(Elm &returnvalue) = 0;

  // Print out the entire list to cout.  Print an appropriate message
  // if the list is empty.  Note:  the "const" keyword indicates that
  // this function cannot change the contents of the list.
  virtual void print() const = 0;

  // Check to see if "value" is in the list.  If it is found in the list,
  // return true, otherwise return false.  Like print(), this function is
  // declared with the "const" keyword, and so cannot change the contents
  // of the list.
  virtual bool find(Elm searchvalue) const = 0;

  // Return the number of items in the list
  virtual int size() const = 0;
};

#endif

非常感谢您的帮助;我们上一个类没有教给我们任何关于继承的知识,但这是这个类的第一个项目,这里也没有教过继承,所以这对我来说都是触手可及的,尽管我设法在谷歌上查找。

最佳答案

您的方法未定义。所以链接器在提示,因为它无法链接到他们的定义。

关于c++ - 收到基于模板类及其子类函数调用的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10082040/

相关文章:

c++ - 无法连接 QPushButton

c++ - 这是参数还是类型?

c++ - 重复的 C++ 模板实例化

c++ - CALLBACK 关键字在 Win-32 C++ 应用程序中意味着什么?

c++ - VS2010-VS2015下编译时如何使用decltype作为较大类型表达式的LHS

c++ - 生成导出包含 ATL::CString 成员的类的 DLL 时出现警告 C4251

c++ - 我无法正确声明这些变量...为什么这些参数不起作用?我只是将 n 值设置为数组长度

c++ - 如何从 Visual C++ 中的版本资源中读取

c++ - 解决 CRTP 函数重载歧义

c++ - 获取MFC对话框成员变量内容