(BST) Error: No matching overloaded function found

(BST) Error: No matching overloaded function found

我刚开始学习二叉搜索树。从昨天开始,我一直在尝试修复一些错误。我查看了参考链接,但无法弄清楚我做错了什么。

下面的示例是从讲师提供给我们的其中一张幻灯片中复制的,但是当我 运行 它时,出现以下错误:

C2672: 'Insert': no matching overloaded function found

C2784: 'void Insert(TreeNode *&,ItemType)': could not deduce template argument for 'TreeNode *&' from 'ItemType *'

如有任何帮助,我们将不胜感激。

BinaryTree.h

#pragma once
using namespace std;

template<class ItemType>
struct TreeNode
{
    ItemType info;
    ItemType* left;
    ItemType* right;
};

template<class ItemType>
class TreeType
{
public:
    TreeType();
    ~TreeType();    

    void InsertItem(ItemType item);

private:
    TreeNode<ItemType>* root;
};

template<class ItemType>                 
void Insert(TreeNode<ItemType>*& tree, ItemType item) // helper function
{
    if (tree == NULL)
    { // insertion place found
        tree = new TreeNode<ItemType>;
        tree->right = NULL;
        tree->left = NULL;
        tree->info = item;
    }
    else if (item < tree->info)
        Insert(tree->left, item); // insert in left subtree
    else
        Insert(tree->right, item); // insert in right subtree
}
template<class ItemType>                 
void TreeType<ItemType>::InsertItem(ItemType item) // member function
{
    Insert(root, item);
}

Main.cpp

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

int main()
{
    TreeType<int> MyTree;
    MyTree.InsertItem(9);
}

template<class ItemType>
struct TreeNode
{
    ItemType info;
    ItemType* left;
    ItemType* right;
};

leftright 是指向存储在 TreeNode 中的数据的指针,而不是指向 TreeNode 的指针。

改为使用

template<class ItemType>
struct TreeNode
{
    ItemType info;
    TreeNode* left;
    TreeNode* right;
};