STL 迭代器继承:'value_type' 没有命名类型

STL Iterator Inheritance : 'value_type' does not name a type

我无法理解此错误的来源:

error: ‘value_type’ in ‘struct std::iterator_traits<sha::Vector<int>::h_iterator>’ does not name a type

我正在尝试创建一个 std::vector 包装器并继承迭代器。 我不明白为什么编译器无法推断出 'value_type' 或 'difference_type'.

这是我的 class 定义:

  template <typename T>
  class Vector
  {
    public:
    explicit Vector(std::initializer_list<T> init) : data(init) {}
    ~Vector() {}

  class h_iterator : std::iterator<std::random_access_iterator_tag, T>
  {
    public:
      h_iterator(typename std::vector<T>::iterator it,
                 Vector<T>* owner) :
        it(it), owner(owner) {}

      T operator *() const { return *it; }
      const h_iterator &operator ++() { ++it; return *this; }
      h_iterator operator ++(int) { h_iterator copy(*this); ++it; return copy; }
      const h_iterator &operator --() { --it; return *this; }
      h_iterator operator --(int) { h_iterator copy(*this); --it; return copy; }
      h_iterator& operator =(const h_iterator& other){ this->it =other.it; return *this;}

      bool operator ==(const h_iterator &other) const { return it == other.it; }
      bool operator !=(const h_iterator &other) const { return it != other.it; }
      bool operator  <(const h_iterator &other) const { return it  < other.it; }
      bool operator  >(const h_iterator &other) const { return it  > other.it; }
      bool operator <=(const h_iterator &other) const { return it <= other.it; }
      bool operator >=(const h_iterator &other) const { return it >= other.it; }

      h_iterator operator +(const long int &delta) const
      {
        h_iterator copy(*this);
        it += delta;
        return copy;
      }

      h_iterator& operator +=(const long int& delta)
      {
        it = it + delta;
        return *this;
      }

      h_iterator operator -(const long int &delta) const
      {
        h_iterator copy(*this);
        it -= delta;
        return copy;
      }

      h_iterator& operator -=(const long int& delta)
      {
        it = it - delta;
        return *this;
      }

      T operator [](const long int &delta) const { return *(it + delta); }

    private:
      typename std::vector<T>::iterator it;  // Iterator
      Vector<T>* owner;                      // Cannot be null as long as the iterator is valid
  };

  // Preserve normal iterator accesses
  typename std::vector<T>::iterator begin() { return data.begin(); }
  typename std::vector<T>::iterator end() { return data.end(); }

  const typename std::vector<T>::iterator cbegin() const { return data.cbegin(); }
  const typename std::vector<T>::iterator cend() const { return data.cend(); }

  // Observale iterator
  h_iterator h_begin() { return h_iterator(data.begin(), this); }
  h_iterator h_end() { return h_iterator(data.end(), this); }

  // Implement Meta-Language functions
  //...

private:
  Vector() {}
  Vector operator=(Vector&) {} // Not Implemented

  std::vector<T> data;            // Vector wrapper
};

编译失败的代码如下:

typedef Vector<int> Container;
typedef Container::h_iterator IT;
typedef std::less<typename 
std::iterator_traits<Vector<int>::h_iterator>::value_type> Compare;

为什么会出现这个编译错误?

当您使用时:

class h_iterator : std::iterator<std::random_access_iterator_tag, T>

基础 class 是 private 基础 class。 h_iterator 的客户端无法访问基础 class 的详细信息。进行推导public.

class h_iterator : public std::iterator<std::random_access_iterator_tag, T>