Possible Duplicate:
C++ Inherited template classes don't have access to the base class
I’m experiencing problems with templates and inheritance. Simply, I have a template class which I want another template class to inherit from. I don’t understand why members of base class are not visible within deriving class? Though without using templates everything works as expected. For example:
template <typename T>
class Foo
{
public:
T x;
T y;
void doX(){ x = x+1; }
void doY(){y++;}
protected:
T a;
T b;
void doA(){a++;}
};
template <typename T>
class Bar : public Foo<T>
{
public:
void doX(){x++; y++;} // ERROR
void doY(){x++; y++;} // ERROR
void doA(){a++;b++;} // ERROR
};
These variables are dependent names (read The Dreaded Two-Phase Name Lookup for details), so you need to use
thisas:Usually
this->is not required as it is implicit. But here it is required. Actually explicitthis->tells the compiler to look up the name in the second phase when the base class is instantiated, as this case uses two-phase name lookup mechanism.Or you could qualify them with base class as:
In this case, the fact that the names cannot be looked-up untill
base(which is a typedef ofFoo<T>) is instantiated, becomes more obvious (to humans eyes) in my opinion.