【问题标题】:visibility of a nested class in an inherited class继承类中嵌套类的可见性
【发布时间】:2012-11-30 23:42:51
【问题描述】:

我处理的代码大致如下:

// List.h
template <typename T> class List{
    template <typename TT> class Node;
    Node<T> *head;
    /* (...) */
    template <bool D> class iterator1{
        protected: Node<T> this->n;
        public: iterator1( Node<T> *nn ) { n = nn }
        /* (...) */
    };
    template <bool D> class iterator2 : public iterator1<D>{
        public:
        iterator2( Node<T> *nn ) : iterator1<D>( nn ) {}
        void fun( Node<T> *nn ) { n = nn; }
        /* (...) */
    };
};

(如需上述具体代码,请参考我的previous question)

// Matrix.h
#include "List.h"
template <typename T>
class Matrix : List<T> {
    /* (...) - some fields */
    class element {
        supervised_frame<1> *source; // line#15
        /* (...) - some methods */
    };
};

我在 g++ 中收到以下错误:

 In file included from main.cpp:2:
 Matrix.h:15: error: ISO C++ forbids declaration of ‘supervised_frame’ with no type
 Matrix.h:15: error: expected ‘;’ before ‘<’ token

【问题讨论】:

  • 这里没有足够的代码来查明错误。但是,您猜测一下,您需要转发声明supervised_frame&lt;int&gt;
  • @Yuushi 请参考提供的链接。我在上面的代码中包含了 List.h,并且在其中定义了 supervised_frame。
  • 谁赞成这个问题,为什么?这完全是荒谬的。 @infoholic_anonymous:请看sscce.org;让你需要帮助的人做所有的工作是没有效率的。
  • @ildjarn 这个标题在我看来非常清楚。不过,他确实应该包含List 的简短定义。
  • @Pubby 谢谢你的评论,我已经按照你的建议做了

标签: c++ templates inheritance visibility nested-class


【解决方案1】:

我相信Matrix&lt;T&gt;::element 类与List&lt;T&gt; 类无关。所以我认为你应该有typename List&lt;T&gt;::template supervised_frame&lt;1&gt;

【讨论】:

  • @infoholic_anonymous,好的,那么我认为它需要是 typename List&lt;T&gt;::template supervised_frame&lt;1&gt; 。这是为了让编译器知道第二个“
【解决方案2】:

和你之前的问题类似——使用typename List&lt;T&gt;::supervised_frame&lt;1&gt; *source; 这是因为supervised_frame&lt;1&gt;是一个依赖类型,即它依赖于模板参数T

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    相关资源
    最近更新 更多