Note that there are some explanatory texts on larger screens.

plurals
  1. POcannot access protected variable in derived class c++
    primarykey
    data
    text
    <p>I have a Binary Search Tree as a derived class from a Binary Tree, right now I am trying to access the root for my recursive functions (which is in the base class). But for some reason I keep getting the error:</p> <pre><code>binSTree.h:31: error: ‘root’ was not declared in this scope </code></pre> <p>Here are my class declarations:</p> <p>base class:</p> <pre><code>template &lt;class T&gt; class binTree { private: int height(treeNode&lt;T&gt;*) const; // Recursive method int size(treeNode&lt;T&gt;*) const; // Recursive method int leaves(treeNode&lt;T&gt;*) const; void insert(treeNode&lt;T&gt;*&amp;, const T&amp;); void clear(treeNode&lt;T&gt;*); treeNode&lt;T&gt;* copy_tree(treeNode&lt;T&gt;*); void preOrder(treeNode&lt;T&gt;*, void (*)(T&amp;)); void inOrder(treeNode&lt;T&gt;*, void (*)(T&amp;)); void postOrder(treeNode&lt;T&gt;*, void (*)(T&amp;)); public: binTree(); binTree(const binTree&lt;T&gt;&amp;); ~binTree(); bool empty() const; void clear(); void insert(const T&amp;); int remove(const T&amp;); // Extra credit only int height() const; // Non-recursive method int size() const; // Non-recursive method int leaves() const; void preOrder(void (*)(T&amp;)); void inOrder(void (*)(T&amp;)); void postOrder(void (*)(T&amp;)); const binTree&lt;T&gt;&amp; operator=(const binTree&lt;T&gt;&amp;); protected: treeNode&lt;T&gt;* root; }; </code></pre> <p>header file (to line 31):</p> <pre><code>#include "binTree.h" template&lt;class T&gt; class binSTree : public binTree&lt;T&gt; { public: void insert(const T&amp;); bool remove(const T&amp;); bool search(const T&amp;, int&amp;) const; private: void insert(treeNode&lt;T&gt;*&amp;, const T&amp;); bool remove(treeNode&lt;T&gt;*&amp;, const T&amp;); bool search(treeNode&lt;T&gt;*, const T&amp;, int&amp;); void remove_root(treeNode&lt;T&gt;*&amp;); }; template&lt;class T&gt; void binSTree&lt;T&gt;::insert(const T&amp; x) { treeNode&lt;T&gt;* newNode = new treeNode&lt;T&gt;(x); insert(newNode, x); } template&lt;class T&gt; // public bool binSTree&lt;T&gt;::remove(const T&amp; x) { return remove(binTree&lt;T&gt;.root, x); } template&lt;class T&gt; // public bool binSTree&lt;T&gt;::search(const T&amp; x, int&amp; len) const { len = 0; len = search(root,x,len); } </code></pre> <p>I tried making the root public to see what would happen, and I still got the same error.</p> <p>any help would be much appreciated! </p>
    singulars
    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