Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ (BST)错误:未找到匹配的重载函数_C++_Templates_Data Structures_Binary Tree_Binary Search Tree - Fatal编程技术网

C++ (BST)错误:未找到匹配的重载函数

C++ (BST)错误:未找到匹配的重载函数,c++,templates,data-structures,binary-tree,binary-search-tree,C++,Templates,Data Structures,Binary Tree,Binary Search Tree,我刚开始学习二进制搜索树。从昨天开始,我一直在努力修正几个错误。我看了参考链接,但我不知道我做错了什么 下面的示例是从讲师提供给我们的一个幻灯片中复制的,但当我运行它时,出现以下错误: :“插入”:未找到匹配的重载函数 :'void Insert(TreeNode*&,ItemType)':无法从'ItemType*'推断'TreeNode*&'的模板参数 任何帮助都将不胜感激 BinaryTree.h #pragma once using namespace std; template<

我刚开始学习二进制搜索树。从昨天开始,我一直在努力修正几个错误。我看了参考链接,但我不知道我做错了什么

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

:“插入”:未找到匹配的重载函数

:'void Insert(TreeNode*&,ItemType)':无法从'ItemType*'推断'TreeNode*&'的模板参数

任何帮助都将不胜感激

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);
}
#pragma一次
使用名称空间std;
模板
树状结构
{
项目类型信息;
ItemType*左;
ItemType*右侧;
};
模板
类树型
{
公众:
TreeType();
~TreeType();
无效插入项(项目类型项目);
私人:
树根;
};
模板
void Insert(TreeNode*&树,ItemType item)//helper函数
{
if(tree==NULL)
{//找到插入位置
树=新树节点;
树->右=空;
树->左=空;
树->信息=项目;
}
else if(项目info)
插入(树->左,项);//在左子树中插入
其他的
插入(树->右,项);//插入右子树
}
模板
void TreeType::InsertItem(ItemType item)//成员函数
{
插入(根,项目);
}
Main.cpp

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

int main()
{
    TreeType<int> MyTree;
    MyTree.InsertItem(9);
}
#包括
#包括“BinaryTree.h”
使用名称空间std;
int main()
{
树形MyTree;
插入项(9);
}
中的

模板
树状结构
{
项目类型信息;
ItemType*左;
ItemType*右侧;
};
left
right
是指向存储在
TreeNode
中的数据的指针,而不是指向
TreeNode
s的指针

改用

模板
树状结构
{
项目类型信息;
TreeNode*左;
TreeNode*对;
};
中的

模板
树状结构
{
项目类型信息;
ItemType*左;
ItemType*右侧;
};
left
right
是指向存储在
TreeNode
中的数据的指针,而不是指向
TreeNode
s的指针

改用

模板
树状结构
{
项目类型信息;
TreeNode*左;
TreeNode*对;
};

旁注:
使用名称空间标准
是,但它在标题中尤其令人讨厌,因为它将风险的选择强加给包括标题在内的每个人。您还希望标题在内部保持一致。h缺乏,这迫使对包含它的文件进行不必要的排序。这导致了神秘的虫子。代码工作正常,有人做了无关紧要的更改,然后出错是,但它在标题中尤其令人讨厌,因为它将风险的选择强加给包括标题在内的每个人。您还希望标题在内部保持一致。h缺乏,这迫使对包含它的文件进行不必要的排序。这导致了神秘的虫子。代码工作正常,有人做出了无关紧要的更改,并出现了错误。感谢您花时间回答。我的课堂讲稿好像有个打字错误。你的建议修正了错误。蓝精灵发生了,@Joe。老实说,这个答案之所以存在,是因为当我在评论中称之为“打字错误”时,我担心评论不够清晰。谢谢你花时间回答。我的课堂讲稿好像有个打字错误。你的建议修正了错误。蓝精灵发生了,@Joe。老实说,这个答案之所以存在,是因为当我在评论中称之为“打字错误”时,我担心评论不够清晰。