I am writing binary search tree program in c++. I have a node class as follows:
template<typename T> class BinaryTreeNode{ private: T _data; BinaryTreeNode<T>* _left; BinaryTreeNode<T>* _right; public: T& data() { return _data; } const T& data() const { return _data; } BinaryTreeNode<T>* left() { return _left; } const BinaryTreeNode<T>* left() const { return _left; } BinaryTreeNode<T>* right() { return _right; } const BinaryTreeNode<T>* right() const { return _right; } }; In my binary search tree class I have a method to add a node as follows
template<typename T> void BinarySearchTree<T>::_push(BinaryTreeNode<T>** root ,BinaryTreeNode<T>* node){ if(*root == NULL){ *root = node; }else{ if(node->data() < (*root)->data()){ _push(&((*root)->left()), node); }else{ _push(&((*root)->right()), node); } } } I am getting following error in _push():
binary_tree.h:32:9: error: lvalue required as unary ‘&’ operand _push(&((*root)->left()), node); ^ binary_tree.h:36:9: error: lvalue required as unary ‘&’ operand _push(&((*root)->right()), node); ^ Is something wrong with this the syntax? Can anybody kindly explain what is wrong here?
template<typename T> void BinarySearchTree<T>::_push(BinaryTreeNode<T>*& root ,BinaryTreeNode<T>* node)leftandrightreturn pointers by value. You can't take address of a temporary. Haven't looked at the logic of your code, I think you simply want to the pointers that those functions return - so just get rid of&.