本文介绍了错误-根据g ++未声明继承的类字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a code that has the following logic. g++ gives me the error that I have not declared n in my iterator2. What could be wrong?

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; }
        /* (...) */
    };
};

编辑:

我附上了实际的头文件。

I attach the actual header file. iterator1 would be iterable_frame and 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


推荐答案

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

Proper two-phase name look-up mandates that non-dependent names (i.e., names not depending on a template argument) are looked up in phase 1. At that point the base class members of a templatized base cannot be seen (because a specialization may yield a different layout). The fix is to make the names dependent. Change

n = nn;

成为

this->n = nn;

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

or, in the expanded example, change

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;
}

这篇关于错误-根据g ++未声明继承的类字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 01:42