【发布时间】:2014-08-01 03:40:31
【问题描述】:
我正在尝试实现一棵红黑树,并且我创建了每个包含左子节点、右子节点和父节点的节点,这些节点作为受保护的数据成员存储在我的 redBlackNode 类中。在我的插入函数中,我需要使用 node->_left->_parent 等访问每个节点受保护的成员,比如它的左子节点或父节点。但是,我的编译器抱怨说
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
我该如何克服这个问题?
【问题讨论】:
-
你熟悉accessors吗?该节点可以向其他人透露它的邻居在哪里。
-
要么提供公共访问器,要么为其他类或函数添加友元声明。
标签: c++ protected private-members red-black-tree