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'或'nower_type'。
这是我的班级定义:
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>
基类是private
基类。h_iterator
客户无法访问基类的详细信息。制作派生public
。
class h_iterator : public std::iterator<std::random_access_iterator_tag, T>
相关文章:
- 在类型和包装器之间reinterpret_cast是否安全<Type>?
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- 在"结构提升::enable_if<提升::is_pod<T>,无效>"中没有名为"type"的类型
- C++ "错误:在'类 std::result_of< ... >"中没有名为'type'的类型"
- Cython:如何从C级类型获取'actual Python type'(类型代码/dtype)
- 作为模板类型参数,为什么 type[N] 与其专用版本不匹配----模板<类 T>类 S<T[]>
- Clang 对使用的类型别名发出"unused type alias"警告
- 检测<T>某些非数字类型 T 的 std::numeric::type 的特化
- 不同指针类型之间的无效比较:'type*' 和 'type&(*)()'
- 按参数类型更改"return type template"
- 在shared_from_this()中的错误类型(在继承类中)(是否有dyn.type-ware shared Poi
- grpc & protobuf -- 错误:std::result_of<> 中没有名为 'type' 的类型
- 类 std::result 中没有名为 "type" 的类型
- 访问基类型数组成员(Int-to-type习惯用法)
- 类类型的"Incomplete type isn't allowed"数组
- `type'和`const type'的类型特征
- 我是否可以保证SizeOf(type)== sizeof(无符号类型)
- 编译错误C++与上面定义的发生错误的类型"does not name a type"?
- 仅当我提供类型检查规则时才"No type checking rule"错误
- 使用公共类函数初始化节点类型<Type>指针C++