0

So in my data structure class, I am writing an inorder traversal function to iterate through the nodes in a BST and push the items into a vector. However, this is the first time I am dealing with callback functions and lambdas. Below is what I have tried and I got a type conversion error. Any insight would be appreciated.

template<class ItemType> // Recursion launcher
void BinarySearchTree<ItemType>::inorderTraverse(void visit(ItemType& item)) const {
    if(rootPtr == nullptr) return;
    inorder(rootPtr, visit);
}

template<class ItemType> // I am not allowed to change this function signature
void BinarySearchTree<ItemType>::inorder(BinaryNode<ItemType>* node, void visit(ItemType& item)) const
{ 
    if(node->left) inorder(node->left, visit);
    ItemType item = node->getItem();
    visit(item);
    if(node->right) inorder(node->right, visit);
}

template<class ItemType>
void BinarySearchTree<ItemType>::function() {
    vector<ItemType> temp;
    // The problem is the function below
    auto function = [&temp](auto item){temp.emplace_back(item);}
    inorderTraverse(function);
}

0 Answers0