问问题
5205 次
2 回答
7
您是否尝试过定义所有必需的类型/运算符?
#include <iterator>
struct nit
{
typedef std::random_access_iterator_tag iterator_category;
typedef int value_type;
typedef int difference_type;
typedef int* pointer;
typedef int& reference;
bool operator==(nit const&)
{
return true;
}
bool operator!=(nit const&)
{
return false;
}
int operator-(nit const&)
{
return 0;
}
nit()
{
}
};
int main()
{
nit const test1;
std::distance(test1, test1);
return 0;
}
于 2012-11-12T14:59:03.983 回答
1
要么,您必须在您的类中提供 std::iterator_traits 期望的所有类型定义(有或没有 std::iterator 的帮助),或者您必须自己专门化 std::iterator_traits。
此版本的 GCC 会发出其他错误消息,但不会改变您的代码非法的事实。
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&)’
于 2012-11-12T14:59:31.110 回答