小编ano*_*non的帖子

C++模板类和继承

可能重复:
[FAQ] 为什么派生模板类不能访问基本模板类的标识符? c ++中基类中受保护字段的问题
无法访问类模板中的数据成员

以下代码给出了编译错误.怎么了?

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
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

谢谢

c++ inheritance templates

8
推荐指数
1
解决办法
1万
查看次数

标签 统计

c++ ×1

inheritance ×1

templates ×1