I want to inherit from a class with a template contructor but it seems i cannot call this contructor the way i want.
The error is not finding a matching Base::Base()
I tried to call the contructor with Base<T>()
but it doesn’t work. it gets me ‘class Base Base::Base’ is not a non-static data member of ‘Derived’ error.
I thought about using Base::Base
or using Base::Base<T>
but it doesn’t help. and my Base class is not a class template so i shouldn’t inherit this way : class Derived : public Base<T>
Why do i use templates, you may ask ? I want to instantiate my Derived class this way : ‘Derived’ so i don’t havec to declare it this way :
Derived d;
d.setFoo(new Foo);
main.cpp
#include "Foo.hpp"
int main(void)
{
Derived<Foo> derived;
return 0;
}
Base.hpp
#include "Foo.hpp"
class Foo;
class Base
{
private:
Foo* m_p_foo;
public:
template <typename T>
Base()
{
m_p_foo = new T(this);
}
~Base();
};
template <typename T>
class Derived : public Base
{
public:
Derived():Base(){};
};
Base.cpp
#include "Base.hpp"
Base::~Base()
{
delete m_p_foo;
};
Foo.hpp
#pragma once
#include "Base.hpp"
class Base;
class Foo
{
Base* m_p_base;
public :
Foo(Base* p_base){m_p_base = p_base;};
~Foo();
};
Foo.cpp
#include "Foo.hpp"
Foo::~Foo()
{
delete m_p_base;
};
Errors :
In file included from src/Foo.hpp:2,
from src/main.cpp:1:
src/Base.hpp: In instantiation of 'Derived<T>::Derived() [with T = Foo]':
src/main.cpp:6:18: required from here
src/Base.hpp:25:20: error: no matching function for call to 'Base::Base()'
25 | Derived():Base(){};
| ^
src/Base.hpp:13:5: note: candidate: 'template<class T> Base::Base()'
13 | Base()
| ^~~~
src/Base.hpp:13:5: note: template argument deduction/substitution failed:
src/Base.hpp:25:20: note: couldn't deduce template parameter 'T'
25 | Derived():Base(){};
| ^
src/Base.hpp:6:7: note: candidate: 'constexpr Base::Base(const Base&)'
6 | class Base
| ^~~~
src/Base.hpp:6:7: note: candidate expects 1 argument, 0 provided