Deriving a class from a class template

后端 未结 3 516
青春惊慌失措
青春惊慌失措 2021-01-21 16:37

I am new to C++. During my learning phase I encountered with below issue. I am trying to derive a class stack from a class template Queue. Compiler thr

3条回答
  •  故里飘歌
    2021-01-21 17:10

    Because the base class is a template, whose instantiation depends on a template parameter of the derived class, and you're trying to name a member of the base class, two-phase lookup mandates that you write this->b, not b.

    (And that default constructor call is not needed.)

    stack()
    {
        this->b = this->a;
    }
    
    void pop()
    {
        this->b--;
    }
    

    (live demo)

    Welcome to C++… :P


    [C++11: 14.6.2/3]: In the definition of a class or class template, if a base class depends on a template-parameter, the base class scope is not examined during unqualified name lookup either at the point of definition of the class template or member or during an instantiation of the class template or member. [..]

提交回复
热议问题