Either, you have to provide all typedefs (with or without the help of std::iterator) in your class that std::iterator_traits is expecting or you have to specialize std::iterator_traits yourself.
This version of GCC emits other error messages but it doesn't change the fact that your code is illegal.
prog.cpp: In function ‘int main()’:
prog.cpp:9: error: uninitialized const ‘test1’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++v4/bits/stl_iterator_base_types.h: At global scope:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_iterator_base_types.h: In instantiation of ‘std::iterator_traits<nit>’:
prog.cpp:10: instantiated from here
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_iterator_base_types.h:133: error: no type named ‘iterator_category’ in ‘class nit’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_iterator_base_types.h:134: error: no type named ‘value_type’ in ‘class nit’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_iterator_base_types.h:136: error: no type named ‘pointer’ in ‘class nit’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_iterator_base_types.h:137: error: no type named ‘reference’ in ‘class nit’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_iterator_base_funcs.h: In function ‘typename std::iterator_traits<_Iterator>::difference_type std::distance(_InputIterator, _InputIterator) [with _InputIterator = nit]’:
prog.cpp:10: instantiated from here
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_iterator_base_funcs.h:119: error: no matching function for call to ‘__iterator_category(nit&)’
std::iterator
. I can imagine thatstd::iterator_traits
is only specialized for things that inherit from that. – Saladeclass nit : public std::iterator<std::random_access_iterator_tag, T, int>
, butT
needs to be a non-void type, and you need to provide anoperator-
for this to work. Alternatively you can have abidirectional_iterator_tag
, but then you need to provide incrementors and comparators. – Salade