简体   繁体   中英

C++ Linker errors when calling constructor (Beginner :-()

I am coming back to c++ after 20 years but the language seems to have evolved a lot. I cannot get this to run. I feel like a student.

This is an AVL tree exercise. The avl code was just downloaded (not mine).

// AVL.H

        #include <iostream>  
        template <class Comparable>
        class AvlTree
        {
          public:
            explicit AvlTree( const Comparable & notFound );
            AvlTree( const AvlTree & rhs );
            ~AvlTree( );

            // other functions ...

            const AvlTree & operator=( const AvlTree & rhs );

          private:
            AvlNode<Comparable> *root;
            const Comparable ITEM_NOT_FOUND;
            ....etc
        };

AVL.CPP

#include "avl.h" // and other system includes
            template <class Comparable>
            AvlTree<Comparable>::AvlTree( const Comparable & notFound ) :
              ITEM_NOT_FOUND( notFound ), root( NULL )
            {
            }

            /**
             * Copy constructor.
             */
            template <class Comparable>
            AvlTree<Comparable>::AvlTree( const AvlTree<Comparable> & rhs ) :
              ITEM_NOT_FOUND( rhs.ITEM_NOT_FOUND ), root( NULL )
            {
               *this = rhs;
            }

            /**
             * Destructor for the tree.
             */
            template <class Comparable>
            AvlTree<Comparable>::~AvlTree( )
            {
                makeEmpty( );
            }

            .... other functions like insert, remove, makeEmpty etc

Calling program (mine)

 #include <iostream> ...other includes
 #include "avl.h"

    class Student
    {
      int id;
      string fname, lname, level;
    public:
      Student::Student(int idx, string fnamex, string lnamex, string levelx) {
        id=idx;
        ...etc
      }
      Student::Student(const Student & rhs) {
        id = rhs.id;
        ... etc
      }
      bool operator< (const Student & rhs ) const
      { return id < rhs.id; }
      bool operator== (const Student & rhs ) const
      { return id == rhs.id; }
      Student operator= ( const Student & rhs ) {
        id = rhs.id;
        ...etc
      }
    };

    int main()
    {
      Student notFound(-1,"","",""); 

      AvlTree<Student> myTree(notFound); 
      system("PAUSE");
      return 0;
    }

When I build in Visual Studio 2008 I get the following errors.

Error 1 error LNK2019: unresolved external symbol
  "public: __thiscall AvlTree<class Student>::~AvlTree<class Student>(void)" (??1?$AvlTree@VStudent@@@@QAE@XZ) referenced in function _main main.obj  AVLTree

Error 2 error LNK2019: unresolved external symbol 
 "public: __thiscall AvlTree<class Student>::AvlTree<class Student>(class Student const &)" (??0?$AvlTree@VStudent@@@@QAE@ABVStudent@@@Z) referenced in function _main main.obj  AVLTree

Error 3 fatal error LNK1120: 2 unresolved externals .....

It looks like it says the destructor and the copy constructor are not defined. But I can see that ARE defined in AVL.CPP.

Please help. I am losing my self-esteem.

When you use templates, you almost always have to declare and define the templates in the header file. You can't put the definitions of function templates or class template member functions into a separate .cpp file because the compiler needs to have the definition available when you use the template.

For more information, see the C++ FAQ Lite article, Why can't I separate the definition of my templates class from its declaration and put it inside a .cpp file?

On most compilers, separating templates into header and implementation files is not supported (for good reason - it is a nightmare to implement from a compiler perspective). Typically when dealing with template classes, you either write it all inline in the header file (most common), or include your implementation file at the end of your header (less common, but useful for classes with complex implementations).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM