I am trying to create a base class in C++ while using a class template. The class only contains a single protected data member (at least for ease of explaining).
I am then trying to access said data member from a derived class.
This works fine when I am not using templates, however as soon as I implement a template I get the following error:
./main.cpp:45:31: error: use of undeclared identifier 'x'
void printX(){ std::cout << x << std::endl; }
Usually I would just change my strategy but templates are required for this assignment, and I feel that I have exhausted all other resources at this point.
Any advice on the matter would be greatly appreciated.
Here is the code that generates the error:
#include <iostream>
template <typename T>
class parent{
protected:
T x;
public:
void setX(T a){ x = a; }
};
template <typename T>
class child : public parent<T>{
public:
void printX(){ std::cout << x << std::endl; }
};
int main(){
child<int> obj;
obj.setX(10);
obj.printX();
return 0;
}