【发布时间】:2011-02-07 08:38:46
【问题描述】:
可能的重复:
[常见问题] 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 template
以下代码给了我编译错误。怎么了?
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
如何解决这个问题?
谢谢
【问题讨论】:
-
这个问题我已经看过好几次了,但是找不到链接。
-
找到了一个,但如果有人能找到一个更好的问题,那就太好了:Problem with protected fields in base class in c++
-
@Chris:这是duplicate,这是lengthy explanation。
-
您是否注意到 D2 是从 D1 私下继承的?不是错误的原因,但可能是另一个错误。
-
@Gorpik- D2 实际上是从 D1 公开继承的,因为它是一个结构并且结构的默认继承模式是公共的。
标签: c++ templates inheritance