我正在学习 boost::iterator_facade。这是一本书的示例代码:
#include <iostream>
#include <boost/iterator/iterator_facade.hpp>
template<typename I,std::ptrdiff_t N = 2>
class step_iterator:
public boost::iterator_facade<
step_iterator<I>,
typename boost::iterator_value<I>::type const,
boost::single_pass_traversal_tag>
{
private:
I m_iter;
public:
step_iterator(I x) :
m_iter(x){}
step_iterator(step_iterator const& other):
m_iter(other.m_iter){}
void operator=(step_iterator const& other)
{ m_iter = other.m_iter; }
private:
friend class boost::iterator_core_access;
reference dereference() const
{ return *m_iter; }
void increment()
{ std::advance(m_iter,N); }
bool equal(step_iterator const& other) const
{ return m_iter == other.m_iter;}
};
这是测试代码:
int main()
{
char s[] = "12345678";
std::copy(s,s+8,
std::ostream_iterator<char>(cout));
cout<<endl;
step_iterator<char*> first(s),last(s+8);
std::copy(first,last,
std::ostream_iterator<char>(cout));
return 0;
}
但是,我收到以下编译错误:
'reference' does not name a type
(perhaps 'typename boost::iterator_facade<step_iterator<I, 2>, const typename boost::iterator_value<Iterator>::type, boost::single_pass_traversal_tag, const typename boost::iterator_value<Iterator>::type&, int>::reference' was intended)
所以我尝试用 step_iterator::reference 替换 'reference',这次我得到以下错误:
type 'step_iterator<I, 2>' is not derived from type 'step_iterator<I, N>'