简体   繁体   中英

(BST) Error: No matching overloaded function found

I just started learning Binary Search Trees. I have been trying to fix a couple of errors since yesterday. I looked at the reference links but I can't figure out what I'm doing wrong.

The example below was copied from one of the slideshows provided to us by the lecturer but when I run it, I get the following errors:

C2672 : 'Insert': no matching overloaded function found

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

Any help would be appreciated.

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);
}

In

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

left and right are pointers to the data being stored in the TreeNode , not pointers to TreeNode s.

Instead use

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM