简体   繁体   English

“struct std::iterator_traits”中没有名为“value_type”的类型

[英]no type named ‘value_type’ in ‘struct std::iterator_traits

I'm trying to implement a simple ostream_itreator, which streams every N-th element, but i get a type error我正在尝试实现一个简单的 ostream_itreator,它流式传输每个第 N 个元素,但我收到一个类型错误

error: no type named ‘value_type’ in ‘struct std::iterator_traits<outIterator<int> >’
       typedef typename iterator_traits<_OI>::value_type _ValueTypeO;

Code:代码:

#include <iostream>
#include <vector>
#include <iterator>


template<class T>
class outIterator {
    std::ostream *stream;
    size_t N;
    const char* delim;
    size_t counter = 0;
public:
    // initialization
    outIterator(std::ostream& out)
    : stream(&out)
    , N(1)
    , delim(" ")
    {}

    outIterator(std::ostream& out, size_t N, const char* delimiter)
    : stream(&out)
    , N(N)
    , delim(delimiter)
    {}

    // =
    outIterator<T>& operator= (const T& value) {
        if (counter % N == 0){
            *stream << value << delim;
        }
        return *this;
    }
};

int main() {
    outIterator<int> out(std::cout, 2, " ");
    std::vector<int> vec {0, 1, 2, 3, 4, 5};
    std::copy(vec.begin(), vec.end(), out);
    return 0;
}

Also i didn't include overloadings ++ and ++(int).另外我没有包括重载++和++(int)。 They increment counter and return *this.他们增加计数器并返回 *this。 And * overloading that returns *this和 * 重载返回 *this

Error description:错误描述:

/usr/include/c++/7/bits/stl_algobase.h:378:57: error: no type named ‘value_type’ in ‘struct std::iterator_traits<outIterator<int> >’
       typedef typename iterator_traits<_OI>::value_type _ValueTypeO;
                                                         ^~~~~~~~~~~
/usr/include/c++/7/bits/stl_algobase.h:383:9: error: no type named ‘value_type’ in ‘struct std::iterator_traits<outIterator<int> >’
       const bool __simple = (__is_trivial(_ValueTypeI)
                             ~~~~~~~~~~~~~~~~~~~~~~~~~~
                       && __is_pointer<_II>::__value
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                       && __is_pointer<_OI>::__value
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
         && __are_same<_ValueTypeI, _ValueTypeO>::__value);
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Custom iterator types must either contain member typedefs for difference_type , value_type , pointer , reference , and iterator_category or specialize std::iterator_traits to provide the same.自定义迭代器类型必须包含用于difference_typevalue_typepointerreferenceiterator_category成员类型定义,或者专门化std::iterator_traits以提供相同的类型。

In your specific case, you should modify your outIterator template like this:在您的特定情况下,您应该像这样修改outIterator模板:

template <typename T>
class outIterator {
//...
public:
    using difference_type = std::ptrdiff_t;
    using value_type = T;
    using pointer = T*;
    using reference = T&;
    using iterator_category = std::output_iterator_tag;
//...
};

When you write your iterator, you have to include some variable.当您编写迭代器时,您必须包含一些变量。 You only need to include these:你只需要包括这些:

template <class T>
class iterator {
    public:
        // code ...   
        using iterator_category = std::forward_iterator_tag;
        using difference_type = std::ptrdiff_t;
        using value_type = T;
        using pointer = value_type*;
        using reference = value_type&;
        // code ...
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++在&#39;struct std :: iterator_traits中没有名为&#39;value_type&#39;的类型 <int> “ - C++ no type named ‘value_type’ in ‘struct std::iterator_traits<int>' cpp std::copy 编译期间出现问题:在“struct std::iterator_traits”中没有名为“value_type”的类型<unsigned_char> - cpp std::copy problems during compilation : no type named 'value_type' in 'struct std::iterator_traits<unsigned_char> 在struct std :: iterator_traits &lt;...&gt;中没有名为“ pointer”的类型 - no type named ‘pointer’ in struct std::iterator_traits<…> 为什么 std::iterator_traits 不能找到 value_type? - Why can't std::iterator_traits find value_type? 迭代器特征中没有名为value_type的类型 - No type named value_type in iterator traits 遇到错误:“struct std::iterator_traits”中没有名为“iterator_category”的类型<std::vector<int> &gt; - Encountring error: no type named ‘iterator_category’ in ‘struct std::iterator_traits<std::vector<int> > “struct std::iterator_traits”中没有名为“iterator_category”的类型<int* const> '</int*> - No type named 'iterator_category' in 'struct std::iterator_traits<int* const>' C ++的value_type是否可以从iterator_traits扩展为所有类型? - Can C++'s value_type be extended from iterator_traits to all types? 为什么不能从iterator_traits获取value_type? - Why can't I get value_type from iterator_traits? 是否会创建一个iterator_traits <InIter> :: value_type传递时会触发一个deference? (例外测试) - Will the creation of an iterator_traits<InIter>::value_type trigger a deference when passed? (Exception testing)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM