i am getting this error "error C2664: 'bool __thiscall BST::insert(class BST_Node *& ,struct Key)' : cannot convert parameter 1 from 'class BST_Node *' to 'class BST_Node *& '
A reference that is not to 'const' cannot be bound to a non-lvalue" and i have no ideal why everything looks fine to me unless im overlooking something.

it points to this

bool BST::insert(BST_Node* &subRoot, Key key)
{
	BST_Node* node = new BST_Node(key);
	if(!subRoot)
	{
		subRoot = node;
		return true;
	}
	if(key == root->getKey())
	{
		return false;
		delete node;
	}
	if(key < subRoot->getKey())
	{
		if(subRoot->getLeft() == NULL)
		{
			root->setLeft(new BST_Node(key));
		}
		else
		{
			insert(subRoot->getLeft(), key.data);
		}
	}
}
#include <iostream>
using namespace std;

// structs/Classes
/*********************************************************************/
struct Key {
	char* data;  // string

	Key();
	Key(char* data) { this->data = new char[strlen(data)];
								strcpy(this->data,data);}
	~Key() {delete data;};
	Key(Key& key);

	operator= (Key& key);
	bool operator== (Key& key) { return 0 == strcmp(this->data,key.data);} // is this == to that key
	bool operator< (Key& key)  { return -1 == strcmp(this->data,key.data);}// is this < that key
	bool operator> (Key& key)  { return 1 == strcmp(this->data,key.data);} // is this > that key
};

Key::Key()
{
	data = NULL;
}

Key::Key(Key& key)
{
	data = key.data;
}

/*********************************************************************/
class BST_Node {
private:
	Key key;         // key holds the data
	BST_Node* left;  // ptr to left subtree
	BST_Node* right; // ptr to right subtree

public:
	// Managers
	BST_Node();
	BST_Node(Key key);        // Construct given key-data
	BST_Node(BST_Node& node); // Copy Constructor
	~BST_Node();              // Destruct node

	// Operators
	BST_Node& operator= (BST_Node& node); // Assignment

	// Accessors
	Key getKey() {return key;};         // get Key Data
	BST_Node* getLeft() {return left;};  // get root of left subtree
	BST_Node* getRight() {return right;}; // get root of right subtree
	void setLeft(BST_Node* node);
	void setRight(BST_Node* node);
};

BST_Node::BST_Node()
{
	key.data = NULL;
	left = NULL;
	right = NULL;
}

BST_Node::BST_Node(Key key)
{	
	
	cout << "enter key" << endl;
	cin >> key.data;

}

BST_Node::BST_Node(BST_Node& node)
{
	right = node.right;
	left = node.left;
	key = node.key;
}

BST_Node::~BST_Node()
{
	delete left;
	delete right;
}

BST_Node& BST_Node::operator= (BST_Node& node)
{
	right = node.right;
	left = node.left;
	key = node.key;
	return *this;
}

void BST_Node::setLeft(BST_Node* node)
{
	this->left = node;
}

void BST_Node::setRight(BST_Node* node)
{
	this->right = node;
}
/*********************************************************************/
class BST {
private:
	BST_Node* root; // root of tree
	int size;       // number of elements in tree

	// Mutators - called by public methods
	bool insert(BST_Node* &subRoot, Key key); // insert to subtree (recursive)
	void preOrder(BST_Node* subRoot);  // preOrder-Traversal of tree (recursive)
	void inOrder(BST_Node* subRoot);   // inOrder-Traversal of tree (recursive)
	void postOrder(BST_Node* subRoot); // postOrder-Traversal of tree (recursive)

public:
	// Managers
	BST();         // Construct Empty Tree
	BST(BST& bst); // Copy Constructor
	~BST();        // Destruct tree
	
	// Operators
	BST& operator= (BST& bst); // Assignment 

	// Accessors
	int getSize() {return size;}; // returns number of elements in tree
	bool empty();  // is tree empty?
	bool full();   // is memory available?
	void preOrder();  // preOrder-Traversal of tree (recursive)
	void inOrder();   // inOrder-Traversal of tree (recursive)
	void postOrder(); // postOrder-Traversal of tree (recursive)
	bool findKey();   // take input from user-keyboard
	bool findKey(Key key, BST_Node* &parent, BST_Node* &foundNode); // given key-data, find node

	// Mutators
	bool insert();         // take input from user-keyboard
	bool insert(Key key);  // insert key-data into tree
	bool delNode();        // take input from user-keyboard
	bool delNode(Key key); // delete node containing key-data from tree
};

BST::BST()
{
	size = 0;
	root = NULL;
}

BST::BST(BST& bst)
{
	root = bst.root;
	size = bst.size;
}

BST& BST::operator= (BST& bst)
{
	root = bst.root;
	size = bst.size;
	return *this;
}

bool BST::insert()
{
	Key key;
	cout << "Enter a key" << endl;
	cin >> key.data;
}

bool BST::insert(BST_Node* &subRoot, Key key)
{
	BST_Node* node = new BST_Node(key);
	if(!subRoot)
	{
		subRoot = node;
		return true;
	}
	if(key == root->getKey())
	{
		return false;
		delete node;
	}
	if(key < subRoot->getKey())
	{
		if(subRoot->getLeft() == NULL)
		{
			root->setLeft(new BST_Node(key));
		}
		else
		{
			insert(subRoot->getLeft(), key.data);
		}
	}
}

bool BST::empty()
{
	return root == NULL;
}

bool BST::full()
{
	BST_Node* bstnode = new BST_Node;
	if(!bstnode)
	{
		return true;
	}
	else
	{
		delete bstnode;
		return false;
	}
}

bool BST::insert(Key key)
{
	insert(root, key);
}

> A reference that is not to 'const' cannot be bound to a non-lvalue
> insert(subRoot->getLeft(), key.data);
At this point, there is no storage location (an l-value) which to take a reference to. All you have is the value (r-value) of the result of subRoot->getLeft()

I would suggest you look into how you pass things around by value or by reference.

Also
> this->data = new char[strlen(data)];
You forgot to allocate space for the \0.
Why not use std::string instead?

> return -1 == strcmp(this->data,key.data);
The standard only requires that strmp() return <0, ==0 and >0
Assuming that it's always -1, 0 and +1 is wrong.

i dont really understand you explanation of y im getting the error could u explain it a little more

It's wanting the equivalent of

BST_Node* temp = subRoot->getLeft();
insert(temp, key.data);

But that would be wrong as well since the pointer which gets updated by the insert() call would be that temp variable, not the left pointer in another instance of a BST_Node.


Perhaps make getLeft() return a reference rather than a pointer?

This article has been dead for over six months. Start a new discussion instead.