accessing protected data members of another class
-
26-12-2019 - |
Question
I am trying to implement a red-black tree, and I have created nodes that each contain a left child node, right child node, and a parent node, stored as protected data members in my redBlackNode class. In my insert function, I need to access each nodes protected members like its left child or its parent, using node->_left->_parent and so on. But, my compiler complains that
bst.h:77:29: error: ‘Node<int, int>* Node<int, int>::_left’ is protected
rbbst.h:160:3: error: within this context
bst.h:77:46: error: ‘Node<int, int>* Node<int, int>::_parent’ is protected
rbbst.h:160:3: error: within this context
How can i overcome this issue?
Solution
my suggestions : 1. recode the class node as a struct; 2. add accessible pubilic member funs that return reference to those data members and you should put the data member as private;
Finally, forgive my poor English. It's my first answer on this web.(●'◡'●)
OTHER TIPS
You can also declare a specific function to have access to a class's private members by using the "friend" keyword.
Are you subclassing the main class. Remeber that in inheritance is C++ only subclasses can access protected protocols. Clearing that out try making the methods public. Please show the error lines and the code for more help.