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 of Derived's template parameters - it's a non-dependent name. The base class where foo() is found, on the other hand - Base<T> - does depend on one of Derived'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() in Derived::foo() as either this->bar() or Base<T>::bar().
This C++ FAQ item explains it nicely: see http://www.parashift.com/c++-faq-lite/templates.html#faq-35.19
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With