【问题标题】:not declared in this scope in the case of inheritance在继承的情况下未在此范围内声明
【发布时间】:2014-04-05 11:53:24
【问题描述】:

我有一个文件my_node.h。在这个文件中,我声明了一个 Stack 类:

template<class Datatype>
class Stack
{
    public:
        Stack() : head( NULL )
        {

        }
        virtual ~Stack()
        {
            Datatype temp;
            while (pop(temp));
            cout << "Stack released all spaces" << endl;
        }
    public:
        virtual int push(Datatype &);
        virtual int pop(Datatype &);
        virtual Datatype* peek();
    protected:
        Node<Datatype> *head;
};

然后,我有另一个名为 new_stack.h 的文件。在这个文件中,我写了一个Stack的继承类,即StackWithDeep。代码如下:

#include "my_node.h"
#include <iostream>
#include <list>

using namespace std;

template<class Datatype>
class StackWithDeep : public Stack<Datatype>
{
    public:
        StackWithDeep(int thre) : Stack<Datatype>()
        {
            stack_deep = 0;
            limited_deep = thre;
        }
        virtual ~StackWithDeep()
        {
        }
    public:
        virtual int push(Datatype &);
        virtual int pop(Datatype &);
        int getdeep()
        {
            return stack_deep;
        }
    private:
        int stack_deep;
        int limited_deep;
};

template<class Datatype>
int StackWithDeep<Datatype>::push(Datatype &new_data)
{
    Node<Datatype> *pt_node = new Node<Datatype>;
    if (pt_node == NULL)
        return -1;
    if (stack_deep != limited_deep)
    {
        pt_node -> addData(new_data);

        if (head == NULL)
            head = pt_node;
        else
        {
            pt_node -> addPrev(*head);
            head = pt_node;
        }
        stack_deep ++;
        return 1;
    }
    else
    {
        delete pt_node;
        return 0;
    }   
    return 0;
}

我想实现一个 push()。但是,当我编译时,编译器说:

In member function ‘virtual int StackWithDeep<Datatype>::push(Datatype&)’:
error: ‘head’ was not declared in this scope

我想我可以使用这个 head 指针,因为它在类 Stack 中受到保护,并且我的新类公开继承。

【问题讨论】:

    标签: c++ inheritance scope


    【解决方案1】:
     Standard says that unqualified names in a template are generally
     non-dependent and must be looked up when the template is defined.
     Since the definition of a dependent base class is not known at that
     time (there may be specialisations of the base class template that
     have not yet been seen), unqualified names are never resolved to
     members of the dependent base class. Where names in the template are
     supposed to refer to base class members or to indirect base classes,
     they can either be made dependent by qualifying them or brought into
     the template's scope with a using-declaration
    

    使用this-&gt;head 即可...

    【讨论】:

      【解决方案2】:

      您的代码流在 VS2012 中运行正常。但在 Ideone.com 中失败了。

      在派生类中使用 head 时,请使用基类对其进行限定,如下所示:Stack&lt;Datatype&gt;::head。这对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-10-27
        • 1970-01-01
        • 2021-09-20
        • 2016-08-09
        • 2019-02-17
        • 2021-01-07
        相关资源
        最近更新 更多