c++classtemplatesinheritance

C++ template class inheritance: calling base class constructor in derived class constructor


I'm trying to inherit a template class with constructors like this:

template <class T>
class C1
{
public:
    int n;
    C1(int a)
    {
        n=a;
    }
};

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

};

Whenever I run it, I get the error:

In constructor C2::C2(int)':

error: class 'C2' does not have a field named 'C1'

If someone could explain to me what I did wrong, I would mostly appreciate it.


Solution

  • You should add the template parameter to base class

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