我的二进制搜索树 c++ 的递归函数中的错误 C2784 无法推断模板参数

error C2784 in recursive functions of my BinarySearch Tree c++ could not deduce template argument

本文关键字:参数 错误 搜索树 二进制 c++ 递归函数 我的 C2784      更新时间:2023-10-16

我的两个递归函数都收到以下错误。我自己似乎找不到问题所在。如果有人能帮我,我很感激。

错误

3 错误 C2784:"void 插入(树节点 *&,项类型)":无法从"项目类型 *"推断出"树节点 *&"的模板参数

错误

1 错误 C2784:"void Destroy(树节点 *&)":无法从"项目类型 *"推断出"树节点 *&"的模板参数

template <class ItemType>
void Destroy(TreeNode<ItemType>*& tree)
{
    if (tree != NULL)
  {
    Destroy(tree->left);
    Destroy(tree->right);
    delete tree;
  }
}
template <class ItemType>
void Insert(TreeNode<ItemType>*& tree, ItemType item)
{
    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>
struct TreeNode
{
    ItemType info;
    ItemType* left;
    ItemType* right;
};

template<class ItemType>
void TreeType<ItemType>::InsertItem(ItemType item)
{
    Insert(root, item);
}

template<class ItemType>
TreeType<ItemType>::~TreeType()
{
    Destroy(root);
}

你忘了把

template <class ItemType>

在定义Insert之前.您也忘记了Destroy右大括号后的分号.当我修复这些错误时,在 GCC 4.9 下编译的东西。

插入/销毁都是帮助程序函数。错误仅在我使用这些函数时发生。

template<class ItemType>
void TreeType<ItemType>::InsertItem(ItemType item)
{
    Insert(root, item);
}

template<class ItemType>
TreeType<ItemType>::~TreeType()
{
    Destroy(root);
}