I am having trouble with my binary search tree...

these are the errors i am getting - it is my print function and the find function

I have not written the code for the other print functions, i need it to compile before i can go on

bintree.h - find function

//-----------------------------------------------------------------------------
        //FIND
//-----------------------------------------------------------------------------
233) template <class ItemType> 
       void TreeType<ItemType>::Find(ItemType& item, bool& found)
{
        Retrieve(root, item, found);
}

template <class ItemType>
void Retrieve(TreeNode<ItemType>* tree, ItemType& item, bool& found)
{
        if (tree == NULL)
                found = false;
        else if (item < tree->info)
                Retrieve(tree->left, item, found);
        else if (item > tree->info)
                Retrieve(tree->right, item, found);
        else
        {
                item = tree->info;
                found = true;
        }
}

bintree.h - PrintInOrder()

//-----------------------------------------------------------------------------
        //PrintInOrder
//-----------------------------------------------------------------------------
265)   template <class ItemType>
          void TreeType<ItemType>::PrintInOrder(std::ostream& outFile) const
{
        PrintTree(root, outFile);
}

template <class ItemType>
void PrintTree(TreeNode<ItemType>* tree, std::ofstream& outFile)
{
        if (tree != NULL)
        {
                PrintTree(tree->left, outFile);
                outFile << tree->info;
                PrintTree(tree->right, outFile);
        }
}

This is how i am trying to implement these in my client code
Tree.cpp - Find

else if (num == 6)
                {
                        cout << "Enter an integer to find in the tree: ";
                        cin >> item;
                                if (tree.Find(item) == true)
                                        cout << item << " was found";
                                else
                                        cout << item << " was not found";
                }

Tree.cpp - PrintInOrder

else if (num == 9)
                {
                        tree.PrintInOrder(std::ostream& outFile);
                        cout << endl;
                }

These are the Errors:
Tree.cpp:95: error: no matching function for call to `TreeType<int>::Find(int&)'
bintree.h:235: note: candidates are: void TreeType<ItemType>::Find(ItemType&, bool&) [with ItemType = int]
Tree.cpp:104: error: no matching function for call to `TreeType<int>::PrintInOrder()'
bintree.h:267: note: candidates are: void TreeType<ItemType>::PrintInOrder(std::ostream&) const [with ItemType = int]
Tree.cpp:116: error: expected primary-expression before '&' token
Tree.cpp:116: error: `outFile' was not declared in this scope
Tree.cpp:116: warning: unused variable 'outFile'

So these are all very self-explanatory. What part of the errors don't you understand?

Tree.cpp:95: error: no matching function for call to `TreeType<int>::Find(int&)'
bintree.h:235: note: candidates are: void TreeType<ItemType>::Find(ItemType&, bool&) [with ItemType = int]

Apparently Find() takes two arguments (a bool reference as a second argument) and doesn't return anything; instead of taking one argument and returning an int.

Tree.cpp:104: error: no matching function for call to `TreeType<int>::PrintInOrder()'
bintree.h:267: note: candidates are: void TreeType<ItemType>::PrintInOrder(std::ostream&) const [with ItemType = int]

You didn't show this code. But it looks like you are calling PrintInOrder(), which takes one argument (an ostream), without any arguments.

Tree.cpp:116: error: expected primary-expression before '&' token
Tree.cpp:116: error: `outFile' was not declared in this scope
Tree.cpp:116: warning: unused variable 'outFile'

You wrote some type and variable name in the middle of the expression, as if you were declaring some variable or something. I have no idea what you were trying to do here.

I figured out the print, but FIND on the other hand is still not working


Tree.cpp: In function `int main()':
Tree.cpp:95: error: no matching function for call to `TreeType<int>::Find(int&)'
bintree.h:237: note: candidates are: void TreeType<ItemType>::Find(ItemType&, bool&) [with ItemType = int]

those are the errors i get, I can't figure out how to call for found, because i'm figuring that that is what I am missing

When I call for a bool found; in my main program, that error message comes up... not sure what I'm doing wrong

This article has been dead for over six months. Start a new discussion instead.