2

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!

2
  • Got another displayItem out there? Compiler's pretty sure displayItem takes a string, not a BinaryNode. A stale forward declaration, perhaps? Commented Dec 5, 2016 at 5:58
  • Oh my goodness, thank you so much. Wow, I've been staring at this project for over 8 hours that I had forgotten about the declaration. It was just that, I updated the definition, but not the declaration. Thank you very, very much. Commented Dec 5, 2016 at 6:15

1 Answer 1

0

error: cannot initialize a parameter of type 'void (*)(BinaryNode, LinkedQueue > &)' with an lvalue of type 'void (string &)' (aka 'void (basic_string, allocator > &)'): type mismatch at 1st parameter ('BinaryNode, LinkedQueue > &' vs 'string &' (aka 'basic_string, allocator > &'))

Break it down!

cannot initialize a parameter of type

A function has been called with a parameter of the wrong type.

'void (*)(BinaryNode, LinkedQueue > &)'

Expected type

with an lvalue of type 'void (string &)'

Provided type

English translation: Function was called with void displayItem(std::string &), not void displayItem(BinaryNode<string, LinkedQueue<int> >& anItem).

Solution: Ensure void displayItem(BinaryNode<string, LinkedQueue<int> >& anItem) is declared ahead of first use. Possibly search for and remove or rename void displayItem(std::string &) to prevent future confusion.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.