Why does this code fail to compile (undeclared identifier 'x', both g++ 4.9 and clang++ 3.5)?
template <class T>
struct base {
int x;
};
template <class U>
struct end : public base<U> {
end() {
x = 5;
}
};
Note: Explicitly specifying this->x
solves the problem.
It does not compile because dependant base classes are ignored during name lookup, and base
is a dependant base.
You can use the this
pointer :
end() {
this->x = 5;
}
Or just explicitly name the base class :
end() {
base::x = 5;
}
Note:
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