Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deriving a template class from a non-template base

Tags:

c++

templates

I'm trying to do have Derived derive from Base:

class Base
{
public:
    Base() {};
};

template <class T>
class Derived : public Base
{
public:
    Derived::Derived()
    {

    }
};

This gives me the following error:

error C3254: 'Derived': class contains explicit override '{ctor}' but does not derive from an interface that contains the function declaration

note: see reference to class template instantiation 'Derived' being compiled

error C3244: 'Derived::Derived(void)': this method was introduced by 'Unknown>' not by 'Base'

I'm totally new to templates, what simple steps am I missing? This seems like a pretty basic thing.

like image 499
Jeffrey Avatar asked Jan 27 '23 03:01

Jeffrey


1 Answers

You need to omit the "Derived::" prefix. You would only use it to refer to a symbol that was inherited, or injected into the namespace of the class Derived. So the only prefix that makes sense is "Base::" here. It has nothing to do with templates.

class Base
{
public:
    Base() {};
};

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

See a working Live Demo.

like image 84
Andreas H. Avatar answered Jan 28 '23 18:01

Andreas H.