为什么在此范围内没有宣布下一个或上一个?

时间:2015-05-19 04:25:13

标签: c++

我正在尝试在C ++中重新创建一个Deque,并且无法弄清楚为什么next和prev没有在Sentinel的范围内声明如下:

template <typename T> class ANode {
  public:
  ANode<T> * next;
  ANode<T> * prev;
};
template <typename U> class Sentinel: public ANode<U>{

  public:
  Sentinel(ANode<U> * n, ANode<U> * p) {
    next = n;
    prev = p;
    next->prev = this;
    prev->next = this;

  }
  Sentinel() {
     next = this;
     prev = this;
  }
};  

1 个答案:

答案 0 :(得分:0)

名称nextprev不在范围内,因为它们取决于模板参数U。如果要引用依赖于模板类型参数的内容,则需要使用this->限定它们以声明它们是与类型相关的名称。将使用情况更改为this->nextthis->prev,一切都应该正常。

默认情况下,您获得非依赖名称查找规则;参见C ++ 11 14.6.3 [temp.nondep] / 1,基本上有你的确切例子:

Standard Text for 14.6.3