Please consider the following scenario:
1) A parameterized base class A<T>
2) A parameterized derived class B<T> deriving from A<T>.
When the derived class' ctor tries to reference the base class, a compilation error occurs:
/** Examines how to invoke the Base class ctor
from the Derived class,
when both classes are templates.
*/
#include <iostream>
using namespace std;
template<typename T>
class A /// base class
{
public:
A(int i)
{
}
};
template<typename T>
class B : public A<T> /// derived class
{
public:
B(int i) :
A {i}
{
}
};
int main()
{
A<int> a {5};
B<int> b {10};
}
Errors:
\main.cpp||In constructor 'B<T>::B(int)':|
\main.cpp|26|error: class 'B<T>' does not have any field named 'A'|
\main.cpp||In instantiation of 'B<T>::B(int) [with T = int]':|
\main.cpp|35|required from here|
\main.cpp|26|error: no matching function for call to 'A<int>::A()'|
\main.cpp|26|note: candidates are:|
\main.cpp|15|note: A<T>::A(int) [with T = int]|
\main.cpp|15|note: candidate expects 1 argument, 0 provided|
\main.cpp|12|note: constexpr A<int>::A(const A<int>&)|
\main.cpp|12|note: candidate expects 1 argument, 0 provided|
\main.cpp|12|note: constexpr A<int>::A(A<int>&&)|
\main.cpp|12|note: candidate expects 1 argument, 0 provided|
||=== Build failed: 2 error(s), 2 warning(s) (0 minute(s), 0 second(s)) ===|
The compiler interprets the B class's ctor as initializing a field A in the B class, rather than as invoking the A class' ctor.
How can this be fixed?
A<T>instead ofAin the lineB(int i): A{i}