0
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

This is what I recieve when trying to compile my code. I've narrowed it down to (i believe) this section of code here:

#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

This is the child class of the SortedList, which is this, in case it's needed..

#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

Thanks so much for any help; our last class taught us nothing of inheritance, but this is project #1 for this class, without being taught inheritance here either, so this is all touch and go for me, despite what I managed to look up on Google.

  • 1
    Template definitions must go in header files. See this FAQ: [How can I avoid linker errors with my template classes?](http://www.parashift.com/c++-faq/templates.html#faq-35.15) – ildjarn Apr 10 '12 at 01:50
  • possible duplicate of [Template class - unresolved external symbol(s)](http://stackoverflow.com/questions/5776862/template-class-unresolved-external-symbols) – ildjarn Apr 10 '12 at 01:51
  • 1
    Another possible template linker error dup: [Why can templates only be implemented in the header file?](http://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file). – Jesse Good Apr 10 '12 at 01:53

2 Answers2

1

Your methods aren't defined. So the linker is complaining because it can't link to their definitions.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • their definition is in the LinkedSortedList.cpp, unless that's not what you're talking about? I can provide that as well, but I figured it was a lot of code to shove down someone's throat. – Chris Wilson Apr 10 '12 at 01:45
  • 1
    Oh, sure. So as some of the comments on your question suggest, your problem is that template methods must be defined in the header file if they are call from outside the same class. So, move your implementations to the bottom of your header file, and you should be all set. – John Zwinck Apr 10 '12 at 02:30
0

Maybe it helps if you placed the definitions of your functions in your header file. This makes it easier for the compiler to resolve these external symbols.

I hope this will help.

Regards, Philinator

Philipp Neufeld
  • 1,053
  • 10
  • 23