I am having some trouble with inheritance between template classes. I started with a simple code that has a parent class and a child class that inherits that parent. The child class is able to access the protected data member of the parent class.
#include <iostream>
using namespace std;
# define LENGTH 10
class Parent {
protected:
int * array[LENGTH] = {NULL};
};
class Child : public Parent {
public:
void addItem(int * item) {
for (int i = 0; i < LENGTH; i++) {
if (array[i] == NULL) {
array[i] = item;
}
}
}
};
int main() {
Child obj;
int *temp = new int(5);
obj.addItem(temp);
}
This compiles and runs just fine. But then I try to do the same thing with template classes.
#include <iostream>
using namespace std;
# define LENGTH 10
template <typename T>
class Parent {
protected:
T * array[LENGTH] = {NULL};
};
template <typename T>
class Child : public Parent<T> {
public:
void addItem(T * item) {
for (int i = 0; i < LENGTH; i++) {
if (array[i] == NULL) {
array[i] = item;
}
}
}
};
int main() {
Child obj;
int *temp = new int(5);
obj.addItem(temp);
}
This second program does not compile, and I get an error that reads: 'array' was not declared in this scope. I don't know why template classes change the way inheritance works. Is there any way to use inheritance like this other than public get/set methods in the parent class?