I've come across a problem in a class hierarchy I couldn't solve so far. Below you get the minimal example where the template Base class itself inherits from another class that is not a template (called AbsoluteBase in the example below). The class Derived then inherits from Base with its template argument also being Bases' template argument:
#include <iostream>
using namespace std;
class AbsolutBase {
protected:
int number;
AbsoluteBase(int _number) {
number = _number;
}
virtual ~AbsoluteBase() {}
virtual void print() const = 0;
};
template <typename T> class Base : virtual public AbsoluteBase {
public:
Base(int _number) : AbsoluteBase(_number) {}
virtual void print() const {
cout << number << endl;
}
};
template <typename T> class Derived : public Base<T> {
public:
Derived(int _number) : Base<T>::Base(_number) {}
};
int main() {
Derived<char> object(100);
object.print();
}
So each constructor calls the constructor of its parent and passes an integer as argument all the way down to AbsoluteBase. But when compiling the code I get:
error: no matching function for call to 'AbsoluteBase::AbsoluteBase()'
note: candidates are: AbsoluteBase::AbsoluteBase(int)
note: candidate expects 1 argument, 0 provided
Making an instance of Base works just fine but when calling its constructor in the initialization list of Derived, the compiler wants AbsolutBase() as constructor even though the integer argument is given. Obviously, when defining a default constructor in AbsoluteBase the print() function outputs garbage as no value has been passed to number.
So something has to be wrong with my call of Base<T>::Base(int) but I can't see what it is. I am grateful for every explanation and help!
Greetings, Benniczek