The following code is giving compilation error:
template <typename T>
class Base
{
public:
void bar(){};
};
template <typename T>
class Derived : public Base<T>
{
public:
void foo() { bar(); } //Error
};
int main()
{
Derived *b = new Derived;
b->foo();
}
ERROR
Line 12: error: there are no arguments to 'bar' that depend on a template parameter, so a declaration of 'bar' must be available
Why is this error coming?
The name
foo()does not depend on any ofDerived‘s template parameters – it’s a non-dependent name. The base class wherefoo()is found, on the other hand –Base<T>– does depend on one ofDerived‘s template parameters (namely,T), so it’s a dependent base class. C++ does not look in dependent base classes when looking up non-dependent names.To resolve this, you need to qualify the call to
bar()inDerived::foo()as eitherthis->bar()orBase<T>::bar().This C++ FAQ item explains it nicely: see http://www.parashift.com/c++-faq-lite/templates.html#faq-35.19