LNK2019未解析的外部符号.创建二叉树

LNK2019 unresolved external symbol. Creating Binary Tree

本文关键字:符号 创建 二叉树 外部 LNK2019      更新时间:2023-10-16

我创建了链表,它没有问题并运行。另外,我必须包括二叉树。它给了我这个错误

1> Generating Code... 1> Skipping... (no relevant changes detected) 1> main.cpp 1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall SDI::BinaryTree::insert(int)" (?insert@BinaryTree@SDI@@QAEHH@Z) referenced in function _main 1>main.obj : error LNK2019: unresolved external symbol "public: int __thiscall SDI::BinaryTree::preOrder(void)" (?preOrder@BinaryTree@SDI@@QAEHXZ) referenced in function _main 1>C:UsersMuugiiDocumentsVisual Studio 2013LinkedListDebugLinkedList.exe : fatal error LNK1120: 2 unresolved externals ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

二进制树.h

IFNDEF SDI_BINARYTREE

定义SDI_BINARYTREE

包括

namespace SDI
{
    class BinaryTree
    {
        class BTNode
        {
        public:
            int data;
            BTNode *left;
            BTNode *right;
        };
    public:
        BinaryTree();
        ~BinaryTree();
        int insert(const int value);
        int preOrder() const;
        int clearTree();
    private:
        BTNode *headPtr;
        int insert(const int value, BTNode *leaf);
        int clearTree(BTNode *leaf) const;
        int preOrder(BTNode *leaf) const;
    };
};
#endif

二进制树.cpp

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

SDI::BinaryTree::BinaryTree()
{
    headPtr = nullptr;
}
SDI::BinaryTree::~BinaryTree()
{
    //clearTree(headPtr);
}
int SDI::BinaryTree::insert(const int value, BTNode *leaf)
{
    if (value < leaf->data)
    {
        if (leaf->left != nullptr)
        {
            insert(value, leaf->left);
        }
        else
        {
            leaf->left = new BTNode;
            leaf->left->data = value;
            leaf->left->left = nullptr;
            leaf->left->right = nullptr;
        }
    }
    else if (value >= leaf->data)
    {
        if (leaf->right != nullptr)
        {
            insert(value, leaf->right);
        }
        else
        {
            leaf->right = new BTNode;
            leaf->right->data = value;
            leaf->right->left = nullptr;
            leaf->right->right = nullptr;
        }
    }
    return 1;
}

主.cpp

#include <string>
#include <iostream>
#include "Linkedlist.h"
#include "BinaryTree.h"
using namespace std;
int main( int argc, const char *argv[])
{
    SDI::LinkedList myList;
    //SDI::BinaryTree myTree;
    int inp;
....
if (inp2 == 'a')
                {
                    int num;
                        cout << "nPlease enter a value to add: ";
                        cin >> num;
                        myTree.insert(num);
                        cout << "nttValue has been successfully savednnn";

                }

我试过在网上查找,找不到。这将是很大的帮助。

如我所见,您有两个带有名称插入的函数。而第一个

int insert(const int value);

仅声明但未定义。

您不清楚错误消息的哪一部分?它清楚地表明您尚未实现两种方法:

int SDI::BinaryTree::insert(const int value) {
    return 0; // the implementation probably needs some patching up
}
int SDI::BinaryTree::preOrder() const {
    return 0; // as does this one
}