1 year ago

#373515

test-img

Ege

Compilation Error About Templated Classes

I have a AVL class like this, it's templated and it gives error in main, I have tried to debug it for hours but it didn't fixed. What can be the problem? What part am I missing? It can be related to constructors but I tried that and it didn't fixed. I'm using Visual Studio 2012 and I feel stuck.

The error is:

1>Ege_HW2.obj : error LNK2019: unresolved external symbol "public: __thiscall AvlTree<struct item>::~AvlTree<struct item>(void)" (??1?$AvlTree@Uitem@@@@QAE@XZ) referenced in function __unwindfunclet$??0AVLSection@@QAE@XZ$0
1>Ege_HW2.obj : error LNK2019: unresolved external symbol "public: __thiscall AvlTree<struct AVLSection>::~AvlTree<struct AVLSection>(void)" (??1?$AvlTree@UAVLSection@@@@QAE@XZ) referenced in function _main
1>fatal error LNK1120: 2 unresolved externals

Header file:

#ifndef AVLTREE_H
#define AVLTREE_H

#include <iostream>
#include <string>
using namespace std;

/* AVL Tree is taken from the lecture slides. */

template <class Comparable>
class AvlTree;

template <class Comparable>
class AvlNode
{
    Comparable element;
    AvlNode* left;
    AvlNode* right;
    int height;

    AvlNode(const Comparable& theElement,
        AvlNode* lt, AvlNode* rt, int h = 0)
        : element(theElement), left(lt), right(rt), height(h) { }

    friend class AvlTree<Comparable>;
};

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

    const Comparable& findMin() const;
    const Comparable& findMax() const;
    const Comparable& find(const Comparable& x) const;
    bool isEmpty() const;
    void printTree() const;

    void makeEmpty();
    void insert(const Comparable& x);
    void remove(const Comparable& x);

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

private:
    AvlNode<Comparable>* root;
    const Comparable ITEM_NOT_FOUND;

    const Comparable& elementAt(AvlNode<Comparable>* t) const;

    void insert(const Comparable& x, AvlNode<Comparable>*& t) const;
    void remove(const Comparable& x, AvlNode<Comparable>*& t) const;

    AvlNode<Comparable>* findMin(AvlNode<Comparable>* t) const;
    AvlNode<Comparable>* findMax(AvlNode<Comparable>* t) const;
    AvlNode<Comparable>* find(const Comparable& x, AvlNode<Comparable>* t) const;
    void makeEmpty(AvlNode<Comparable>*& t) const;
    void printTree(AvlNode<Comparable>* t) const;
    AvlNode<Comparable>* clone(AvlNode<Comparable>* t) const;

    // Avl manipulations
    int height(AvlNode<Comparable>* t) const;
    int max(int lhs, int rhs) const;
    void rotateWithLeftChild(AvlNode<Comparable>*& k2) const;
    void rotateWithRightChild(AvlNode<Comparable>*& k1) const;
    void doubleWithLeftChild(AvlNode<Comparable>*& k3) const;
    void doubleWithRightChild(AvlNode<Comparable>*& k1) const;
};

#include "AvlTree.cpp"
#endif

Cpp file:

#include "avlTree.h"
#include <iostream>

using namespace std;

template <class Comparable>
AvlTree<Comparable>::AvlTree(){
    root = NULL;
}

/*** Construct the tree.
  */
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)
    : root(NULL), ITEM_NOT_FOUND(rhs.ITEM_NOT_FOUND) {
    *this = rhs;
}

And finally the main:

#include <iostream>
#include <string>
#include "BinaryTree.h"
#include "AvlTree.h"
#include <fstream>
using namespace std;

struct item{
    string title;
    string information;
    item(){};

};

struct AVLSection{
    AvlTree<item> avlSection;
    string sectionTitle;
    AVLSection(){};
};

struct BSTSection{
    BinarySearchTree <item> bstSection;
    string sectionTitle;
    BSTSection(){}
};


void readData(BinarySearchTree<BSTSection> & bst, AvlTree<AVLSection> & avl){
    ifstream input;
    string filename = "data.txt",line;
    input.open(filename.c_str());
    while(getline(input,line)){
        cout << line;
    }
}

int main(){
    BinarySearchTree<BSTSection> bst;
    AvlTree<AVLSection> avl;
}

c++

lnk2019

0 Answers

Your Answer

Accepted video resources