I have this little sample class here:
#include <iostream>
class A {
protected:
void method_a() {
std::cout << "Hello World" << std::endl;
}
};
template <class T>
class B : public A {
public:
void method_b() {
method_a();
}
};
template <class T>
class C : public B<T> {
public:
void method_c() {
method_a(); // the line the question is about
}
};
int main() {
C<int> c;
c.method_c();
}
If I compile this using g++ I get this error:
$ tmp g++ -std=c++17 test.cpp
test.cpp: In member function ‘void C<T>::method_c()’:
test.cpp:22:9: error: there are no arguments to ‘method_a’ that depend on a template parameter, so a declaration of ‘method_a’ must be available [-fpermissive]
method_a();
^~~~~~~~
test.cpp:22:9: note: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)
However if I change the method_c
to the following it works:
void method_c() {
this->method_a();
}
I am having trouble understanding the compiler behaviour here. Why do I need to add a this
here? Is this a general case for c++ and inheriting multiple times? I do not recall having encountered this problem before.
Side note: I actually encountered this in a more complex example but for simplicity I tried to find a minimum working example