C++ 插入自定义对象的二进制搜索树插入函数

C++ 插入自定义对象的二进制搜索树插入函数,c++,binary-search-tree,custom-object,C++,Binary Search Tree,Custom Object,我正在尝试构建一个二进制搜索树,它允许我插入一个自定义对象。我了解如何实现一个接受int和char等基本类型的树,但不确定在对自定义对象执行此操作时会遇到什么问题 以下是所有相关代码: Node.h文件 template <class T> class node { public: T data; node<T> *left; node<T> *right; node(

我正在尝试构建一个二进制搜索树,它允许我插入一个自定义对象。我了解如何实现一个接受int和char等基本类型的树,但不确定在对自定义对象执行此操作时会遇到什么问题

以下是所有相关代码:

Node.h文件

template <class T>
class node {
    public: 
        T data;
        node<T> *left;
        node<T> *right;
        
        node(T data) {
            this->data = data;
            this->left = NULL;
            this->right = NULL;
        }
}
模板
类节点{
公众:
T数据;
节点*左;
节点*右;
节点(T数据){
这->数据=数据;
此->左=空;
此->右=空;
}
}
A.h文件

#ifndef A_H
#define A_H
#include <cstddef>
#include <cstring>

class A {
    public: 
        long long key;
        std::string value;
    
        A() {
            this->key = 0;
            this->value = "";
        }

        A(long long key, std::string value) {
            this->key = key;
            this->value = value;
        }

        bool operator<(const A &other) {
            bool check = this->key < other.key;
            return check;
        }
};
\ifndef A\u H
#定义一个
#包括
#包括
甲级{
公众:
长键;
std::字符串值;
(){
该->键=0;
此->值=”;
}
A(长键,标准::字符串值){
这个->键=键;
这个->值=值;
}
布尔操作
BST.h文件

#ifndef BST_H
#define BST_H

template <class T>
class BST {
    T *root;

    public:
        BST();
        void insert(T item);
    // ... all other methods not included
}
\ifndef BST\u H
#定义BST_H
模板
BST级{
T*根;
公众:
BST();
无效插入(T项);
//…不包括所有其他方法
}
这是我的CPP实现 BST.cpp

#包括“bst.h”
#包括“Node.h”
#包括
#包括
#包括
使用名称空间std;
模板
BST::BST();{
root=NULL;
}
模板
无效BST::插入(T项)
{
插入2(根,项);
}
模板
void insert2(节点*&根,T项){
if(root==NULL){
根=新节点(项);
} 
else if(项data){
插入2(根->左,项目);
} 
否则{
插入2(根->右侧,项目);
}
}
我的主要方法是:

#include <iostream>
#include "bst.h"
#include "bst.cpp"
#include "UPC.h"
#include <cstring>
using namespace std;

int main() {
    // this is for testing purposes
    BST<A> *tree = new BST<A>();
    A *a1 = new UPC(1111111111, "hello");
    node<A> *node1 = new node<A>(*a1);

    tree->insert(*a1);
}
#包括
#包括“bst.h”
#包括“bst.cpp”
#包括“UPC.h”
#包括
使用名称空间std;
int main(){
//这是为了测试目的
BST*树=新的BST();
A*a1=新的UPC(1111,“你好”);
节点*node1=新节点(*a1);
树->插入(*a1);
}
这是我的终端:

bst.cpp: In instantiation of 'void BST<T>::insert(T*) [with T = A]':
main.cpp: required from here
**error**: no matching function for call to 'insert2(A*&, A*&)'
     insert2(root, item);
bst.cpp... note: candidate: 'template<class T> void insert2(node<T>*&, T)'
 void insert2(node<T> *&root, T item) {
              ^~~~~~~
bst.cpp note:   template argument deduction/substitution failed:
bst.cpp note:   'UPC' is not derived from 'node<T>'
     insert2(root, item);
bst.cpp:void bst::insert(T*)[with T=A]的实例化中:
main.cpp:从这里开始需要
**错误**:调用“insert2(A*&,A*&)”时没有匹配的函数
插入2(根,项);
英国夏令时。。。注:候选者:“模板void insert2(节点*&,T)”
void insert2(节点*&根,T项){
^~~~~~~
bst.cpp注意:模板参数推断/替换失败:
bst.cpp注意:“UPC”不是从“节点”派生的
插入2(根,项);
bst.cpp: In instantiation of 'void BST<T>::insert(T*) [with T = A]':
main.cpp: required from here
**error**: no matching function for call to 'insert2(A*&, A*&)'
     insert2(root, item);
bst.cpp... note: candidate: 'template<class T> void insert2(node<T>*&, T)'
 void insert2(node<T> *&root, T item) {
              ^~~~~~~
bst.cpp note:   template argument deduction/substitution failed:
bst.cpp note:   'UPC' is not derived from 'node<T>'
     insert2(root, item);