Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 646
Vasile Turcu Avatar asked Oct 19 '25 05:10

Vasile Turcu


1 Answers

You should add the template parameter to base class

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

};
like image 99
Shangtong Zhang Avatar answered Oct 21 '25 20:10

Shangtong Zhang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!