Compiler error when calling base constructor when base and derived templates with derived type parameter

I'm trying to figure out why the following code won't compile:

template <class T>
class Base {
    public:
        Base(int a){}
};
template <class T>
class Derived: public Base<T>  {
    public:
        Derived(int a): Base(a){}
};
int main(){}

      

In my compiler (gcc 5.4.0 with C ++ 11) the error is displayed

error: class 'Derived<T>' does not have any field named 'Base'
         Derived(int a): Base(a){}

      

I see it more like a call to the base template designer in the participant list initialization error , although this test case is actually compiled for me until then: The main difference is that, as Base

well as Derived

using the same setting. Also, it compiles fine if I explicitly add type parameters or if I provide an explicit scope for the base, as in

template <class T>
class Base {
    public:
        Base(int a){}
};

template <class T>
class Derived: public Base<T>  {
    public:
        Derived(int a): Derived::Base(a){}
};

int main(){}

      

What's happening? I don't understand when the entered class names can be used?

+3


source to share


1 answer


The class name entered Base

is a member of the class Base

, and because the base class depends, its scope is not scoped when looking for an unqualified name. So the use of the name Base

will define the class template, not the name of the injected class Base<T>

. This is why you should write Base<T>

.



Derived::Base

works because it invokes a deferred lookup for the name until Derived

it is created.

+4


source







All Articles