I'm having issues with accessing protected members in template subclasses of a template class.
Thing is, if I remove the templates, everything works fine. I know how to work around my problem in an ugly way, by simply writing "using Base::my_protected_member" for every single derived class. But why should I need to do that in the first place ?
Here's a minimal commented example
template<class T>
class Base
{
protected:
T x;
public:
Base(T x) : x(x) {}
};
template<class T>
class Derived : public Base<T>
{
//using Base<T>::x; //why ?!
public:
Derived(T x);
void print();
};
template<class T>
Derived<T>::Derived(T x) : Base<T>(x)
{ cout << x << endl; } //works fine
template<class T>
void Derived<T>::print() { cout << x << endl; } //doesn't work
int main()
{
Derived<int> d(4);
d.print();
}
I don't quite understand what's going on, or how to work around this in a nice way. probably I'm doing something wrong...