I'm trying to run a function on each node traversed in a Binary Search Tree class I made. Here is the function that traverses the nodes of the BST and runs the function passed in as an argument on each node:
template<class ItemType, class OtherType>
void BinarySearchTree<ItemType, OtherType>::Inorder(void visit(BinaryNode<ItemType, OtherType>&), BinaryNode<ItemType, OtherType>* node_ptr) const {
if (node_ptr != nullptr) {
Inorder(visit, node_ptr->GetLeftPtr());
BinaryNode<ItemType, OtherType> node = *node_ptr;
visit(node);
Inorder(visit, node_ptr->GetRightPtr());
} // end if
} // end inorder
This is a private member function of the BST class, so it gets called by a public member function:
template<class ItemType, class OtherType>
void BinarySearchTree<ItemType, OtherType>::InorderTraverse(void visit(BinaryNode<ItemType, OtherType>&)) const
{
Inorder(visit, root_);
} // end inorderTraverse
In my main file, I created this function to pass in as the argument:
void displayItem(BinaryNode<string, LinkedQueue<int> >& anItem)
The traversal is called like this:
tree1Ptr->InorderTraverse(displayItem);
When I compile, I get this error, and I have no idea how to fix it.
MainBST.cpp:62:29: error: cannot initialize a parameter of type 'void
(*)(BinaryNode<std::__1::basic_string<char>, LinkedQueue<int> > &)' with
an lvalue of type 'void (string &)' (aka 'void (basic_string<char,
char_traits<char>, allocator<char> > &)'): type mismatch at 1st parameter
('BinaryNode<std::__1::basic_string<char>, LinkedQueue<int> > &' vs
'string &' (aka 'basic_string<char, char_traits<char>, allocator<char> >
&'))
tree1Ptr->InorderTraverse(displayItem);
^~~~~~~~~~~
./BinarySearchTree.h:42:29: note: passing argument to parameter 'visit' here
void InorderTraverse(void visit(BinaryNode<ItemType, OtherType>&)) const;
If anyone understands the error and can decipher it and help me, I will be very grateful. If you need me to drop more code, I'll be glad to do it. Thank you so much!
displayItemout there? Compiler's pretty suredisplayItemtakes astring, not aBinaryNode. A stale forward declaration, perhaps?