C++外部类被继承后,内部类问题

现在有个 类①

img


protected: 然后里面有个Node类② (类②在 类①里面)

img

现在有另外一个类③ 继承了 类①

img

然后我在类③ 的 一个函数中 声明一个 类② 指针 ,它显示 找不到标识符

img

就算我把 类②继承了 也不管用

而且我疑问 如果不加模板, 类B继承类A ,那么类B 可以直接 用类A的成员(public的 和protected的)。用了模板 还要 写 A::成员 才能访问,不然找不到。主要是 我继承的时候就已经 把模板写上了 ,用的时候还要写名字 加冒号,这什么逻辑

我现在边学数据结构边学点算法 练习写数据结构的时候经常遇到C++ 这种难受的问题把我搞蒙,翻书也查不到,然后写算法题写不出来,看了解析 时刻感觉自己是 sha 杯 ,操作系统还一堆的概念,你们大一学计算机也这样吗? 我现在感觉我不适合学 这个。

你这个编译错误之前有其它编译错误引起的。Node是dependent type, 你应该在BinarySearchTree<eletype>::Node之前加上typename,你可以这样写。

template <typename eletype>
class BalanceBST : public BinarySearchTree<eletype>
{
    typedef typename BinarySearchTree<eletype>::Node node_type;
    void LL(node_type *pNode)
    {
        node_type *pLeft;
    }
};

FROM https://en.cppreference.com/w/cpp/language/dependent_name#The_typename_disambiguator_for_dependent_names

The typename disambiguator for dependent names

In a declaration or a definition of a template, including alias template, a name that is not a member of the current instantiation and is dependent on a template parameter is not considered to be a type unless the keyword typename is used or unless it was already established as a type name, e.g. with a typedef declaration or by being used to name a base class.

你的类2必须声明为public或者protected才可以被继承,私有变量不被继承