Note that there are some explanatory texts on larger screens.

plurals
  1. POCreating a new Node for a binary search tree
    primarykey
    data
    text
    <p>For a school project I am trying to make a binary search tree at the same time we are supposed to learn how to use 'friendship' in classes. The errors I get while compiling are: [I put comments in code where the errors originate from for clarity] (Keep in mind I am not allowed to nest Node in the BST class they are both supposed to be in separate files and classes for the sake of this programming assignment)</p> <pre><code>BST.cpp: In member function `void BST::insert(std::string, std::string)': BST.cpp:51: error: non-lvalue in assignment BST.cpp:58: error: non-lvalue in assignment BST.cpp:62: error: non-lvalue in assignment makefile.txt:9: recipe for target `BST.o' failed make: *** [BST.o] Error 1 </code></pre> <p>I tried using the 'new' operator in BST.cpp and Node.cpp but I still can't get rid of those error messages. I believe that I might be missing a few syntax that is making the compiler not like it. Here are the files used in this assignment: (Note some functions arent used yet since I have not gotten that far in the project.) Node.h</p> <pre><code>#ifndef NODE_H_INCLUDED #define NODE_H_INCLUDED #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; class BST; class Node { public: Node(string key, string data) {m_key = key; m_data = data;} ~Node(); static string get_key(); //takes in ptr to node and returns its key static string get_data(); //takes in ptr to node and returns its data static Node* get_left(); //takes in ptr to node and returns its left child pointer static Node* get_right(); //takes in ptr to node and returns its right child pointer static Node* get_parent(); //takjes in ptr to node and returns its parent pointer static Node* create_node(string key, string data); static void destroy_node(); private: string m_key; string m_data; Node *m_left; Node *m_right; Node *m_parent; }; #endif // NODE_H_INCLUDED </code></pre> <p>Node.cpp</p> <pre><code>#include "Node.h" static string Node::get_key() { return m_key; } static string Node::get_data() { return m_data; } static Node* Node::get_left() { return m_left; } static Node* Node::get_right() { return m_right; } static Node* Node::get_parent() { return m_parent; } static Node* Node::create_node(string key, string data) { Node* ptr = new Node(key, data); ptr-&gt;m_left = NULL; ptr-&gt;m_right = NULL; ptr-&gt;m_parent = NULL; return ptr; } </code></pre> <p>My intent so far is to have Node::create_Node to make a new node, Nullify all the pointers, and lastly pass the pointer of the node back to BST.cpp so the pointers can be modified and inserted into the tree. Below are BST.cpp and BST.h (I put comments where the errors occur for your clarity) BST.h:</p> <pre><code>#ifndef BST_H_INCLUDED #define BST_H_INCLUDED #include &lt;iostream&gt; #include &lt;string&gt; using namespace std; class BST { public: BST() {m_root = NULL;} ~BST(); void insert(string key, string data); void find(string key); void remove(string key, string data); void print(); friend class Node; private: Node* m_root; }; #endif // BST_H_INCLUDED </code></pre> <p>Finally, BST.cpp (where the errors occur) The errors happen when I attempt to modify the pointers of z (z is a pointer the brand new node that was just created) including its m_left, m_right and m_parent.</p> <pre><code>#include "BST.h" #include "Node.h" void BST::insert(string key, string data) { Node* x = m_root; Node* y = NULL; Node* z = Node::create_node(key, data); while(x != NULL) { y = x; if(key &lt; x-&gt;get_key()) { x = x-&gt;get_left(); } else { x = x-&gt;get_right(); } } z-&gt;get_parent() = y; //error: non-lvalue in assignment if(y == NULL) { m_root = z; } else if(z-&gt;get_key() &lt; y-&gt;get_key()) { y-&gt;get_left() = z; //error: non-lvalue in assignment } else { y-&gt;get_right() = z; //error: non-lvalue in assignment } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload