2

I am trying to recreate a Deque in C++, and cannot figure out why next and prev aren't declared in the scope of Sentinel as follows:

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 Answers1

0

The names next and prev aren't in scope there because they depend on the template parameter U. If you want to refer to something that depends on a template type parameter, you need to qualify them with this-> to declare that they are intended to be type-dependent names. Change the uses to this->next and this->prev and all should be well.

By default you get non-dependent name lookup rules; see C++11 14.6.3 [temp.nondep]/1 which basically has your exact example:

Standard Text for 14.6.3

Billy ONeal
  • 104,103
  • 58
  • 317
  • 552