Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to retrieve data out of a private string
    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:11: error: `get_key' undeclared (first use this function) BST.cpp:11: error: (Each undeclared identifier is reported only once for each function it appears in.) BST.cpp: At global scope: BST.cpp:5: warning: unused parameter 'data' makefile.txt:9: recipe for target `BST.o' failed make: *** [BST.o] Error 1 </code></pre> <p>I want to be able to access the functions in Node.cpp to be able to retrieve its private members for the sake of the binary search tree. In BST.cpp so far I am attempting to compare a string 'key' that is passed into the 'insert' function with the string that 'xPtr' is currently pointing to. The classes are defined as: Node.h (directly below)</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(); string get_key(Node *ptr); //takes in ptr to node and returns its key string get_data(Node *ptr); //takes in ptr to node and returns its data Node* get_left(Node *ptr); //takes in ptr to node and returns its left child pointer Node* get_right(Node *ptr); //takes in ptr to node and returns its right child pointer private: string m_key; string m_data; Node *m_left; Node *m_right; }; #endif // NODE_H_INCLUDED </code></pre> <p>Node.cpp</p> <pre><code>#include "Node.h" string Node::get_key(Node* ptr) { return ptr-&gt;m_key; } string Node::get_data(Node* ptr) { return ptr-&gt;m_data; } Node* Node::get_left(Node* ptr) { return ptr-&gt;m_left; } Node* Node::get_right(Node* ptr) { return ptr-&gt;m_right; } </code></pre> <p>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>BST.cpp</p> <pre><code>#include "BST.h" #include "Node.h" void BST::insert(string key, string data) { Node* yPtr = NULL; Node* xPtr = m_root; while(xPtr != NULL) { yPtr = xPtr; if(key &lt; get_key(xPtr)) //error: 'get_key' undeclared (first use this function) { } } } </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.
    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