duplicados posibles:
[FAQ]Why doesn't a derived template class have access to a base template class' identifiers? Problem with protected fields in base class in c++
cannot access data member in a class templateclase de C++ plantilla y de herencia
siguiente código me da error de compilación. ¿Qué está mal?
struct Base {
int amount;
};
template<class T> struct D1 : public Base {
};
template<class T>
struct D2 : D1<T> {
void foo() { amount=amount*2; /* I am trying to access base class data member */ };
};
int main() {
D2<int> data;
};
test.cpp: In member function 'void D2<T>::foo()':
test.cpp:11: error: 'amount' was not declared in this scope
¿Cómo corregir esto?
gracias
He visto esta pregunta varias veces pero no puedo encontrar un enlace. –
Encontré uno, aunque si alguien puede encontrar uno con una pregunta mejor que sería genial: [Problema con los campos protegidos en la clase base en C++] (http://stackoverflow.com/questions/1813671/problem-with-protected- fields-in-base-class-in-c) –
@Chris: Aquí hay un [duplicado] (http://stackoverflow.com/questions/4210108/cannot-access-data-member-in-alassiculation) , y aquí hay una [explicación larga] (http://stackoverflow.com/questions/4643074/why-do-i-have-to-access-template-base-class-members-through-the-this-pointer). – GManNickG