Inheritance problem---How do I access to members of child class in c++{myRoot in this case}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
  

//bst.h file
using namespace std;

template <typename T>
class BST
{
public:
    BST();
    
    //T getleaf(const BSTNode *temp) const;
    
    bool empty() const;
    
    bool search(const T& item) const; // recursive
    
    void insert(const T& item); // non recursive
    
protected:
    class BSTNode
    {
    public:
        T data;
        
        BSTNode *left;
        
        BSTNode *right;
        
        BSTNode()
        : left(NULL), right(NULL)
        {
            //myRoot = NULL;
        }
        
        BSTNode(T item)
        : data(item), left(NULL), right(NULL){}
    };
private:
    
    bool search(const T element, BSTNode *current) const;
    
protected:
    BSTNode * myRoot;
    
};



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

avl.h file

#include "bst.h"
using namespace std;

template <typename T>
class avl : BST< T>
{
public:
    avl(T item);
protected:
    void rotate_left(const T& item) const;
    void rotate_right(const T& item) const;
    void rotate_leftRight(const T& item) const;
    void rotate_rightLeft(const T& item) const;
    int getHighDifferent(const T& item) const;
    
    
};

template <typename T>
avl<T>::avl(T item)
{
    BST<T>::insert(item);
}

template <typename T>
void avl<T>::rotate_left(const T& item) const
{
    
}

template <typename T>
int avl<T>::getHighDifferent(const T& item) const
{
    BSTNode * temp = myRoot; // error
    
    
}

#endif  


I got an error at LINE #37.
Thanks
Last edited on
You got "an error"? Okay, we have "an answer". Done. Case solved?


No? What is the exact error message then?
(We cannot simply click "compile" with the code that you did post, so our compilers
cannot immediately spit out a message.)


One might be able to transfer the problem to somebody else with: auto temp = myRoot;
BSTNode is a subtype of a dependent base class. myRoot is further a dependent name. The line may appear like this:
typename BST<T>::BSTNode* temp = this->myRoot;

This is a little ugly - you may want to do some reading about the rules regarding the dependent name lookup mechanism.

There are two problems:
1.) you must specify this->myRoot because the name myRoot does not depend on a template parameter, and so it is checked according to the nondependent lookup rules at the point of declaration in the current context. Introducing the this pointer makes the name implicitly dependent.
2.) you must specify typename BST<T>::BSTNode* because BSTNode is again a dependent name (which is looked up when T becomes known); you must add typename to disambiguate in case there is a template specialization for a given T where BST<T>::BSTNode does not name a type.

See:
http://en.cppreference.com/w/cpp/language/dependent_name
http://en.cppreference.com/w/cpp/language/lookup

P.S.: that makes my 1000th post! ;)
Last edited on
Topic archived. No new replies allowed.