C ++ LNK2019不知道什么是错的

时间:2016-01-09 17:46:39

标签: c++

1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall BSNode<int>::BSNode<int>(int)" (??0?$BSNode@H@@QAE@H@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall BSNode<int>::~BSNode<int>(void)" (??1?$BSNode@H@@QAE@XZ) referenced in function "public: void * __thiscall BSNode<int>::`scalar deleting destructor'(unsigned int)" (??_G?$BSNode@H@@QAEPAXI@Z)
1>main.obj : error LNK2019: unresolved external symbol "public: void __thiscall BSNode<int>::insert(int)" (?insert@?$BSNode@H@@QAEXH@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall BSNode<int>::getData(void)" (?getData@?$BSNode@H@@QAEHXZ) referenced in function "void __cdecl printData<int>(class BSNode<int> *,class std::basic_ofstream<char,struct std::char_traits<char> > &)" (??$printData@H@@YAXPAV?$BSNode@H@@AAV?$basic_ofstream@DU?$char_traits@D@std@@@std@@@Z)
1>main.obj : error LNK2019: unresolved external symbol "public: class BSNode<int> * __thiscall BSNode<int>::getLeft(void)" (?getLeft@?$BSNode@H@@QAEPAV1@XZ) referenced in function "void __cdecl printData<int>(class BSNode<int> *,class std::basic_ofstream<char,struct std::char_traits<char> > &)" (??$printData@H@@YAXPAV?$BSNode@H@@AAV?$basic_ofstream@DU?$char_traits@D@std@@@std@@@Z)
1>main.obj : error LNK2019: unresolved external symbol "public: class BSNode<int> * __thiscall BSNode<int>::getRight(void)" (?getRight@?$BSNode@H@@QAEPAV1@XZ) referenced in function "void __cdecl printData<int>(class BSNode<int> *,class std::basic_ofstream<char,struct std::char_traits<char> > &)" (??$printData@H@@YAXPAV?$BSNode@H@@AAV?$basic_ofstream@DU?$char_traits@D@std@@@std@@@Z)
1>main.obj : error LNK2019: unresolved external symbol "public: class BSNode<int> * __thiscall BSNode<int>::getRoot(void)" (?getRoot@?$BSNode@H@@QAEPAV1@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: bool __thiscall BSNode<int>::remove(int)" (?remove@?$BSNode@H@@QAE_NH@Z) referenced in function _main

我收到这些错误,我不明白为什么。

这是代码: BSNode.cpp

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

template<class T>
BSNode<T>::BSNode(T data)
{
    _root = this;
    insert(data);
}

template<class T>
BSNode<T>::~BSNode()
{
    delete _right;
    delete _left;
}

template<class T>
void BSNode<T>::rebalance(BSNode<T> * n)
{
    setBalance(n);

    if (n - _balance == -2)
    {
        if (getHeight(n->_left->_left) >= getHeight(n->_left->_right))
        {
            n = rotateRight(n);
        }
        else
        {
            n = rotateLeftThenRight(n)
        }
    }
    else if (n->_balance == 2)
    {
        if (getHeight(n->_right->_right) >= getHeight(n->_right->_left))
        {
            n = rotateLeft(n);
        }
        else
        {
            n = rotateRightThenLeft(n);
        }
    }

    if (n->_parent = NULL)
    {
        rebalance(n->_parent);
    }
    else
    {
        root = n;
    }
}

template<class T>
void BSNode<T>::setBalance(BSNode<T> *n)
{
    n->_balance = getHeight(n->_right) - getHeight(n->_left);
}

template<class T>
BSNode<T>* BSNode<T>::rotateLeft(BSNode<T>* a)
{
    BSNode<T>* b = a->right;
    b->parent = a->_parent;
    a->_right = b->_left;

    if (a->_right != NULL)
    {
        a->_right->_parent = a;
    }

    b->_left;
    a->_parent = b;

    if (b->_parent != NULL)
    {
        if (b->_parent->_right == a)
        {
            b->_parent->_right = b;
        }
        else
        {
            b->_parent->left = b;
        }
    }

    setBalance(a);
    setBalance(b);
    return b;
}

template<class T>
BSNode<T>* BSNode<T>::rotateRight(BSNode<T>* a)
{
    BSNode<T> *b = a->_left;
    b->_parent = a->_parent;
    a->_left = b->_right;

    if (a->_left != NULL)
    {
        a->_left->_parent = a;
    }

    b->_right = a;
    a->_parent = b;

    if (b->_parent != NULL)
    {
        if (b->_parent->_right == a)
        {
            b->_parent->_right = b;
        }
        else
        {
            b->_parent->_left = b;
        }
    }
    setBalance(a);
    setBalance(b);
    return b;

}

template<class T>
BSNode<T>* BSNode<T>::rotateLeftThenRight(BSNode<T> *n)
{
    n->_left = rotateLeft(n->_left);
    return rotateRight(n);
}

template<class T>
BSNode<T>* BSNode<T>::rotateRightThenLeft(BSNode<T> *n)
{
    n->_right = rotateRight(n->_right);
    return rotateLeft(n);
}

template<class T>
void BSNode<T>::insert(T data)
{
    if (_root == NULL)
    {
        _root = new BSNode<T>(data);
        _root->_data = data;
    }
    else
    {
        BSNode<T>* n = _root, *parent;

        while (true)
        {
            if (n->key == key)
            {
                return false;
            }
            parent = n;

            bool goLeft = n->_data > data;
            n = goLeft ? n->_left : n->_right;

            if (n == NULL)
            {
                if (goLeft)
                {
                    parent->_left = new BSNode(data);
                    parent->_left->_data = data;
                }
                else
                {
                    parent->_right = new BSNode(data);
                    parent->_right->_data = data;
                }
                rebalance(parent);
                break;
            }
        }
    }
    return true;
}

template<class T>
T BSNode<T>::getData()
{
    return _data;
}

template<class T>
BSNode<T>* BSNode<T>::getLeft()
{
    return _left;
}

template<class T>
BSNode<T>* BSNode<T>::getRoot()
{
    return _root;
}

template<class T>
BSNode<T>* BSNode<T>::getRight()
{
    return _right;
}

template<class T>
BSNode<T>::BSNode(const BSNode<T>& other)
{
    this->_data = other._data;
    this->_right = other._right;
    this->_left = other._left;
}

template<class T>
int BSNode<T>::getHeight() 
{
    int rightLength = 0;
    int leftLenght = 0;

    if (_right)
    {
        rightLength = _right->getHeight();
    }
    if (_left)
    {
        leftLenght = _left->getHeight();
    }

    if (leftLenght > rightLength)
    {
        return leftLenght + 1;
    }
    else
    {
        return rightLength + 1;
    }
}

template<class T>
int BSNode<T>::getDepth()
{
    if (&root == NULL)
    {
        return 0;
    }
    int right = getDepth(*root._right);
    int left = getDepth(*root._left);

    if (left > right)
    {
        return left + 1;
    }
    else
    {
        return right + 1;
    }

}

template<class T>
bool BSNode<T>::search(T val)
{
    if (val == this->_data)
    {
        return true;
    }
    else if (val < this->_data)
    {
        if (!_left)
        {
            return false;
        }
        else
        {
            return _left->search(val);
        }
    }
    else if (val > this->_data)
    {
        if (!_right)
        {
            return false;
        }
        else
        {
            return _right->search(val);
        }
    }
    return false;
}

template<class T>
bool BSNode<T>::isLeaf()
{
    if (this)
    {
        return true;
    }
    return false;
}

template<class T>
BSNode<T>& BSNode<T>::operator=(const BSNode& other)
{
    this->_data = other._data;
    this->_right = other._right;
    this->_left = other._left;
    return *this;
}

template<class T>
bool BSNode<T>::remove(const T key)
{
    if (root == NULL)
        return false;

    BSNode *n = _root, *parent = _root, *delNode = NULL, *childNode = _root;

    while (childNode != NULL)
    {
        parent = n;
        n = childNode;
        childNode = key >= n->_data ? n->_right : n->_left;
        if (key == n->_data)
        {
            delNode - n;
        }
    }

    if (delNode != NULL)
    {
        delNode->_data = n->_data;

        child = n->_left != NULL ? n->_left : n->_right;

        if (_root->_data == key)
        {
            _root = child;
        }
        else
        {
            if (parent->_left == n)
            {
                parent->getLeft = child;
            }
            else
            {
                parent->_right = child;
            }

            rebalance(parent);
        }
    }
    return true;
}

Main.cpp的

#include "BSNode.h"
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#include "printTreeToFile.h"
using namespace std;


int main()
{
    BSNode<int>* bs = new BSNode<int>(6);
    bs->getRoot()->insert(2);
    bs->getRoot()->insert(8);
    bs->getRoot()->insert(3);
    bs->getRoot()->insert(5);
    bs->getRoot()->insert(9);
    bs->getRoot()->insert(6);
    bs->getRoot()->insert(10);
    bs->getRoot()->insert(11);
    bs->getRoot()->insert(12);
    bs->getRoot()->insert(13);
    bs->getRoot()->insert(14);
    bs->getRoot()->insert(20);
    bs->getRoot()->insert(21);
    bs->getRoot()->insert(22);
    bs->getRoot()->insert(23);
    bs->getRoot()->insert(17);
    bs->getRoot()->insert(18);
    bs->getRoot()->insert(1);
    bs->getRoot()->insert(16);
    bs->getRoot()->insert(15);
    bs->getRoot()->insert(24);
    bs->getRoot()->insert(25);
    bs->getRoot()->insert(27);
    bs->getRoot()->insert(26);
    bs->getRoot()->insert(28);
    bs->getRoot()->insert(29);
    bs->getRoot()->insert(30);
    bs->getRoot()->insert(31);
    bs->getRoot()->insert(32);
    bs->getRoot()->insert(33);
    bs->getRoot()->insert(34);
    bs->getRoot()->insert(35);
    bs->getRoot()->insert(36);
    bs->getRoot()->insert(37);
    bs->getRoot()->insert(38);
    bs->getRoot()->insert(39);
    bs->getRoot()->insert(40);
    bs->getRoot()->insert(41);
    bs->getRoot()->insert(42);
    bs->getRoot()->insert(43);
    bs->getRoot()->insert(44);



    bs->getRoot()->remove(1);
    bs->getRoot()->remove(9);
    bs->getRoot()->remove(10);
    bs->getRoot()->remove(11);
    bs->getRoot()->remove(26);


    string textTree = "BSTData.txt";
    printTreeToFile(bs->getRoot(), textTree);
    system("BinaryTree.exe");

    system("pause");
    remove(textTree.c_str());
    delete bs;
    return 0;
}

我正在尝试实现一个AVL树并且有这些链接器错误,我不知道它们为什么会发生,我真的不知道如何解决它们。

1 个答案:

答案 0 :(得分:0)

模板函数的定义必须在使用它的位置可见。实际上,这意味着BSNode.cpp中的东西应该在BSNode.h中。