LNK2019:未解析的外部符号public

时间:2015-10-11 15:48:49

标签: c++ visual-studio-2013 lnk2019

我试图在CodeBlocks中第二次在Visual Studio 2013中编写我的代码,我在尝试安排二进制搜索树时遇到错误, 这是我的代码 BinarySearchTree.h

#pragma once
template <typename T>
class BinarySearchTree{
private:
    struct Node{
        Node *left;
        Node *right;
        T data;
    };
    Node *root;
public:
    BinarySearchTree();
    bool isEmptyTree() const;
    void insertInBinarySearchTree(T& value);
    void removeFromBinarySearchTree(T& value);
    void printBinarySearchTree();
    void inorderBinarySearchTree(Node *p);
};

这是BinarySearchTree:

#include "BinarySearchTree.h"
#include <iostream>
#include <new>
using namespace std;

template <typename T>
BinarySearchTree<T>::BinarySearchTree(){
    root = nullptr;
}

template <typename T>
void BinarySearchTree<T>::insertInBinarySearchTree(T& value){
    Node *t = new Node;
    Node *parent;
    t->data = value;
    t->left = nullptr;
    t->right = nullptr;
    parent = nullptr;
    if (isEmptyTree()) root = t;
    else{
        Node *currentNode;
        currentNode = root;
        while (currentNode){
            parent = currentNode;
            if (t->data, currentNode->data) currentNode = currentNode->right;
            else currentNode = currentNode->left;
        }
        if (t->data < parent->data) parent->left = t;
        else parent->right = t;
    }
}
template <typename T>
void BinarySearchTree<T>::removeFromBinarySearchTree(T& value){
    bool isElementFound = false;
    if (isEmptyTree()){
        cout << "This tree is empty!" << endl;
        return;
    }
    Node *currentNode;
    Node *parent;
    currentNode = parent;
    while (currentNode != nullptr){
        if (currentNode->data == value){
            isElementFound = true;
            break;
        }
        else{
            parent = currentNode;
            if (value > currentNode->data) currentNode = currentNode->right;
            else currentNode = currentNode->left;
        }
    }
    if (!isElementFound()){
        cout << "Data not found in tree!" << endl;
        return;
    }
    if ((currentNode->left == nullptr && currentNode->right != nullptr) || (currentNode->left != nullptr && currentNode->right == nullptr)){
        if (currentNode->left = nullptr && currentNode->right != nullptr){
            if (parent->left == currentNode){
                parent->left = currentNode->right;
                delete currentNode;
            }
            else{
                parent->right = currentNode->right;
                delete currentNode;
            }
        }
        else{
            if (parent->left == currentNode){
                parent->left = currentNode->left;
                delete currentNode;
            }
            else{
                parent->right = currentNode->left;
                delete currentNode;
            }
        }
        return;
    }

    if (currentNode->left == nullptr && current->right == nullptr){
        if (parent->left == currentNode) parent->left == nullptr;
        else parent->right == nullptr;
        delete currentNode;
        return;
    }
    if (currentNode->left == nullptr && currentNode->right == nullptr){
        Node *checker;
        checker = currentNode->right;
        if ((checker->left == nulptr) && (checker->right == nullptr)){
            currentNode = checker;
            delete checker;
            currentNode->right = nullptr;
        }
        else{
            if ((currentNode->right)->left != nullptr){
                Node *leftCurrentNode;
                Node *leftCurrentNodeParent;
                leftCurrentNodeParent = currentNode->right;
                leftCurrentNode = (currentNode->right)->left;
                while (leftCurrentNode->left != nullptr){
                    leftCurrentNode = leftCurrentNodeParent;
                    leftCurrentNode = leftCurrentNode->left;
                }
                currentNode->data = leftCurrentNode->data;
                delete leftCurrentNode;
                leftCurrentNodeParent->left = nullptr;
            }
            else{
                Node *temporaryNode;
                temporaryNode = currentNode->right;
                currentNode->data = temporaryNode->data;
                currentNode->right = temporaryNode->right;
                delete temporaryNode;
            }
        }
        return;
    }
}
template <typename T>
void BinarySearchTree<T>::printBinarySearchTree(){
    inorderBinarySearchTree(root);
}
template <typename T>
void BinarySearchTree<T>::inorderBinarySearchTree(Node *p){
    if (p != nullptr){
        if (p->left) inorderBinarySearchTree(p->left);
        cout << " " << p->data << " ";
        if (p->right) inorderBinarySearchTree(p->right);
    }
    return;
}
template <typename T>
bool BinarySearchTree<T>::isEmptyTree() const{
    return root == nullptr;
}

最后是main.cpp:

#include <iostream>
#include "BinarySearchTree.h"
using namespace std;

int main()
{
    BinarySearchTree<int> BST;
    int ch, tmp, tmp1;
    while (1)
    {
        cout << endl << endl;
        cout << " Binary Search Tree Operations " << endl;
        cout << " ----------------------------- " << endl;
        cout << " 1. Insertion/Creation " << endl;
        cout << " 2. Removal " << endl;
        cout << " 3. Print " << endl;
        cout << " 4. Exit " << endl;
        cout << " Enter your choice : ";
        cin >> ch;
        switch (ch)
        {
        case 1: cout << " Enter Number to be inserted : ";
            cin >> tmp;
            BST.insertInBinarySearchTree(tmp);
            break;
        case 2: cout << " Enter data to be deleted : ";
            cin >> tmp1;
            BST.removeFromBinarySearchTree(tmp1);
            break;
        case 3: cout << endl;
            cout << " Print tree " << endl;
            cout << " -------------------" << endl;
            BST.printBinarySearchTree();
            break;
        case 4:
            return 0;
        }
    }
}

我发现这个错误导致缺少实现,但是我无法在没有实现的情况下找到函数。

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

您遇到的问题是在main.cpp中,您使用int作为模板实例化了您的类,因此在头文件中,所有内容都被视为int。例如。 void insertInBinarySearchTree( int& value );

但是,在实现文件中,仍然使用更通用的模板定义来定义函数,并且在编译时main.cpp将要查找

的函数定义

void insertInBinarySearchTree( int& value );

它失败的原因是因为在cpp文件中你只有

void insertInBinarySearchTree( T& value );

为了解决这个问题,只需转到cpp的最后一行并添加以下内容:

template class BinarySearchTree<int>;

问题将得到修复,因为已经添加了一个处理整数的定义。

P.S:查看代码,看起来===的使用不正确。请记住,=用于分配,==用于检查两个值是否相同。还有许多其他错误,因此我修复了我注意到的here