0

when I try to implement a Binary tree with a few essential functions, there were no error pop up. However, when I initialize the tree and try to use its functions, there's a compile error showing, and I don't know how to fix it, can you help me. The following is my code and the error that shows: Edit: I fix the problem by changing the argument of addItem(T& item) to addItem(const T& item). Thank you for your help!

Node.h

 #ifndef BINARYTREE_NODE_H
 #define BINARYTREE_NODE_H
    
    template <class T>
    class Node {
    public:
        Node<T>* left;
        Node<T>* right;
        T data;
    }; 

BinaryTree.h

#ifndef BINARYTREE_BINARYTREE_H
#define BINARYTREE_BINARYTREE_H

#include "Node.h"
enum Traversal
{
    INORDER,
    POSTORDER,
    PREORDER
};
template <class T>
class BinaryTree {
private:
    Node<T>* root;
    int size = 0;
    Traversal currentTraversal;
    void inOrder(Node<T>* parent,void f(T&));
    void preOrder(Node<T>* parent,void f(T&));
    void postOrder(Node<T>* parent,void f(T&));
    Node<T>* insert(Node<T>* parent, T item);
public:
    void setTraversal(Traversal order);
    void addItem(const T& item);
    BinaryTree();
    void print(T& item);
};

#include "BinaryTree.cpp"
#endif //BINARYTREE_BINARYTREE_H

BinaryTree.cpp

#ifndef BINARYTREE_BINARYTREE_CPP
#define BINARYTREE_BINARYTREE_CPP
#include <iostream>
#include "BinaryTree.h"
template <class T>
BinaryTree<T>::BinaryTree() {
    root = nullptr;

}
template <class T>
Node<T>* BinaryTree<T>::insert(Node<T> *parent, T item) {
        if(parent == nullptr)
        {
            Node<T>* temp = new Node<T>;
            temp->data = item;
            return temp;
        }
        else if(parent->data > item)
        {
            if (parent->left != nullptr)
                insert(parent->left,item);
            else{
                parent->left = new Node<T>;
                parent->left->data = item;
                parent->left->left = nullptr;
                parent->left->right = nullptr;
            }
        }
        else if (parent->data <= item)
        {
            if (parent->right != nullptr)
                insert(parent->right,item);
            else{
                parent->right = new Node<T>;
                parent->right->data = item;
                parent->right->left = nullptr;
                parent->right->right = nullptr;
            }
        }
}
template <class T>
void BinaryTree<T>::addItem(constT &item) {
    insert(root,item);
    size++;
}
template <class T>
void BinaryTree<T>::inOrder(Node<T> *parent, void f(T&))
{
       if (parent != nullptr)
       {
           inOrder(f, parent->left);
           f(parent->data);
           inOrder(f,parent->right);
       }
}
template <class T>
void BinaryTree<T>::preOrder(Node<T> *parent, void f(T&)) {
    if (parent != nullptr)
    {
        f(parent->data);
        preOrder(f,parent->left);
        preOrder(f,parent->right);
    }
}
template <class T>
void BinaryTree<T>::postOrder(Node<T> *parent, void f(T&)) {
    if (parent != nullptr)
    {
        postOrder(f,parent->left);
        postOrder(f,parent->right);
        f(parent->data);
    }
}
template <class T>
void BinaryTree<T>::print(T& item)
{
    std::cout << item <<std::endl;
    if(currentTraversal == PREORDER)
        preOrder(root,print(item));
    if (currentTraversal == POSTORDER)
        postOrder(root,print(item));
    if(currentTraversal == INORDER)
        inOrder(root,print(item));
}
template <class T>
void BinaryTree<T>::setTraversal(Traversal order) {
    currentTraversal = order;
}
#endif

main.cpp

#include <iostream>
#include "BinaryTree.h"
int main() {
   BinaryTree<int> tree;
   tree.addItem(2); // Here's the error showing: "Non-const lvalue reference to type 'int' cannot bind to a temporary of type 'int'"

}

1 Answers1

0

You have:

T &item

As the argument to addItem(). It should either be a const reference or a plain value. That is, const T & or T.

For more on why this is the case, see: How come a non-const reference cannot bind to a temporary object?

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Imagine if this code were allowed to compile and a later modification to `addItem` added the line `++item;` to it. What do you think would happen to in `tree.addItem(2);`? Would that `2` in there -- a constant -- somehow become a 3? You have to tell the compiler that `addItem` will not modify the object passed to it by reference. – David Schwartz Nov 01 '20 at 08:15