【问题标题】:error - inherited class field undeclared according to g++错误 - 根据 g++ 未声明的继承类字段
【发布时间】:2012-12-13 04:06:52
【问题描述】:

我有一个具有以下逻辑的代码。 g++ 给了我没有在我的 iterator2 中声明 n 的错误。有什么问题?

template <typename T> class List{
    template <typename TT> class Node;
    Node<T> *head;
    /* (...) */
    template <bool D> class iterator1{
        protected: Node<T> 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; }
        /* (...) */
    };
};

编辑:

我附上了实际的头文件。 iterator1 将是 iterable_frame 和 iterator2 - switchable_frame。

#ifndef LST_H
#define LST_H

template <typename T>
class List {
    public:
    template <typename TT> class Node;
    private:
    Node<T> *head; 
    public:
    List() { head = new Node<T>; }
    ~List() { empty_list(); delete head; }
    List( const List &l );

    inline bool is_empty() const { return head->next[0] == head; }
    void empty_list();

    template <bool DIM> class iterable_frame {
        protected:
        Node<T> *head; 
        Node<T> **caret;
        public:
        iterable_frame( const List &l ) { head = *(caret = &l.head); }
        iterable_frame( const iterable_frame &i ) { head = *(caret = i.caret); }
        ~iterable_frame() {}
    /* (...) - a few methods follow */
        template <bool _DIM> friend class supervised_frame;
    };
    template <bool DIM> class switchable_frame : public iterable_frame<DIM> {
        Node<T> *main_head;
        public:
        switchable_frame( const List& l ) : iterable_frame<DIM>(l) { main_head = head;     }
        inline bool next_frame() {
            caret = &head->next[!DIM];
            head = *caret;
            return head != main_head;
        }
    };
    template <bool DIM> class supervised_frame {
        iterable_frame<DIM> sentinels;
        iterable_frame<DIM> cells;
        public:
        supervised_frame( const List &l ) : sentinels(l), cells(l) {}
        ~supervised_frame() {}
    /* (...) - a few methods follow */
    };

    template <typename TT> class Node {
        unsigned index[2];
        TT num;
        Node<TT> *next[2];
        public:
        Node( unsigned x = 0, unsigned y = 0 ) {
            index[0]=x; index[1]=y;
            next[0] = this; next[1] = this;
        }
        Node( unsigned x, unsigned y, TT d ) {
            index[0]=x; index[1]=y;
            num=d;
            next[0] = this; next[1] = this;
        }
        Node( const Node &n ) {
            index[0] = n.index[0]; index[1] = n.index[1];
            num = n.num;
            next[0] = next[1] = this;
        }
        ~Node() {}
        friend class List;
    };
};
#include "List.cpp"

#endif

确切的错误日志如下:

In file included from main.cpp:1:
List.h: In member function ‘bool List<T>::switchable_frame<DIM>::next_frame()’:
List.h:77: error: ‘caret’ was not declared in this scope

【问题讨论】:

  • 派生类仍然看不到 private 成员。
  • @chris 确实,我已根据您的评论将父类中字段的可见性更改为受保护,但问题仍然存在。
  • 还有其他错误吗?或者只是关于未声明的head
  • @KarthikT 好吧,有但与这段代码无关。我已经发布了确切的相关部分。

标签: c++ templates inheritance compiler-errors


【解决方案1】:

正确的两阶段名称查找要求在阶段 1 中查找非依赖名称(即不依赖于模板参数的名称)。此时模板化基的基类成员无法看到 (因为专业化可能会产生不同的布局)。解决方法是使名称依赖。改变

n = nn;

成为

this->n = nn;

或者,在扩展示例中,更改

inline bool next_frame() {
    caret = &head->next[!DIM];
    head = *caret;
    return head != main_head;
}

成为

inline bool next_frame() {
    this->caret = &head->next[!DIM];
    head = *this->caret;
    return head != main_head;
}

【讨论】:

    【解决方案2】:

    您可以将head 用作this-&gt;head 并查看它是否有效?来自this post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-29
      • 1970-01-01
      相关资源
      最近更新 更多