【问题标题】:inheriting template class c++继承模板类 c++
【发布时间】:2013-07-25 18:32:55
【问题描述】:

继承和模板问题我的派生类无法识别 x,它是我的基类中的成员变量。

 template <class type>
 class one
 {
 public:
 type getX();
 type x;
 };

 template <class type>
 type one<type>::getX()
 {
 return x;
 }



 template <class type>
 class two: public one <type>
 {
 public:
 type getX();
 };

 template <class type>
 type two<type>::getX()
 {
   return x;
 }

【问题讨论】:

标签: c++ templates inheritance


【解决方案1】:

由于two 是一个模板,而x 不是两个的直接成员,因此您需要明确依赖关系。

一种方式是one&lt;type&gt;::x,另一种可以是this-&gt;x

【讨论】:

    【解决方案2】:

    您必须使用“this->”来访问模板父类中的模板基类成员和函数。

    返回这个->x;

    【讨论】:

      猜你喜欢
      • 2012-10-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多