可能重複:
[常見問題]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 templateC++模板類和繼承
下面的代碼給我的編譯錯誤。哪裏不對?
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++中的基類中的保護字段的問題](http://stackoverflow.com/questions/1813671/problem-with-protected-字段在基類在C) –
@Chris:這是一個[重複](http://stackoverflow.com/questions/4210108/cannot-access-data-member-in-a-class-template) ,這裏是一個[冗長的解釋](http://stackoverflow.com/questions/4643074/why-do-i-have-to-access-template-base-class-members-through-the-this-指針)。 – GManNickG