我想迭代几个文件的行。所以我制作了line_iterator
,然后尝试将其与boost::range::join
结合使用:
class line {
std::string data;
friend std::istream& operator>>(std::istream& is, line& line) {
return std::getline(is, line.data);
}
public:
const std::string& string() const {
return data;
}
};
typedef std::istream_iterator<line> line_iterator;
std::ifstream f3("test/threelines.txt");
std::ifstream f4("test/fourlines.txt");
boost::range::line_range lr3(line_iterator(f3), line_iterator());
boost::range::line_range lr4(line_iterator(f4), line_iterator());
boost::range::line_range lines = boost::range::join(lr3, lr4);
但我收到错误&#34;错误:没有名为&#39; iterator_category&#39;在&#39;类线&#39;&#34; (下面的完整错误)。我认为这是因为join
需要知道这是否是转发/ bidi / etc迭代器,但如何将iterator_category
添加到line
类?
g++-4.8 -Wall -Werror -Wno-unused-local-typedefs -L/Users/astacy/boost/lib -I/Users/astacy/boost/include -fopenmp -lboost_filesystem -lboost_system -c -o obj/test.o src/test.cpp
In file included from /usr/local/Cellar/gcc48/4.8.2/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2/include/c++/bits/stl_algobase.h:65:0,
from /usr/local/Cellar/gcc48/4.8.2/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2/include/c++/bits/char_traits.h:39,
from /usr/local/Cellar/gcc48/4.8.2/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2/include/c++/ios:40,
from /usr/local/Cellar/gcc48/4.8.2/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2/include/c++/ostream:38,
from /usr/local/Cellar/gcc48/4.8.2/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2/include/c++/iostream:39,
from src/test.cpp:1:
/usr/local/Cellar/gcc48/4.8.2/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2/include/c++/bits/stl_iterator_base_types.h: In instantiation of 'struct std::iterator_traits<line>':
/Users/astacy/boost/include/boost/detail/iterator.hpp:81:8: required from 'struct boost::detail::iterator_traits<line>'
/Users/astacy/boost/include/boost/iterator/iterator_traits.hpp:30:75: required from 'struct boost::iterator_value<line>'
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp:141:49: required from 'class boost::iterator_range<line>'
src/test.cpp:22:14: required from here
/usr/local/Cellar/gcc48/4.8.2/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2/include/c++/bits/stl_iterator_base_types.h:165:53: error: no type named 'iterator_category' in 'class line'
typedef typename _Iterator::iterator_category iterator_category;
^
/usr/local/Cellar/gcc48/4.8.2/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2/include/c++/bits/stl_iterator_base_types.h:166:53: error: no type named 'value_type' in 'class line'
typedef typename _Iterator::value_type value_type;
^
/usr/local/Cellar/gcc48/4.8.2/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2/include/c++/bits/stl_iterator_base_types.h:167:53: error: no type named 'difference_type' in 'class line'
typedef typename _Iterator::difference_type difference_type;
^
/usr/local/Cellar/gcc48/4.8.2/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2/include/c++/bits/stl_iterator_base_types.h:168:53: error: no type named 'pointer' in 'class line'
typedef typename _Iterator::pointer pointer;
^
/usr/local/Cellar/gcc48/4.8.2/lib/gcc/x86_64-apple-darwin13.0.0/4.8.2/include/c++/bits/stl_iterator_base_types.h:169:53: error: no type named 'reference' in 'class line'
typedef typename _Iterator::reference reference;
^
In file included from /Users/astacy/boost/include/boost/iterator/iterator_categories.hpp:15:0,
from /Users/astacy/boost/include/boost/iterator/detail/facade_iterator_category.hpp:7,
from /Users/astacy/boost/include/boost/iterator/iterator_facade.hpp:14,
from /Users/astacy/boost/include/boost/range/detail/join_iterator.hpp:20,
from /Users/astacy/boost/include/boost/range/join.hpp:15,
from src/test.cpp:6:
/Users/astacy/boost/include/boost/mpl/eval_if.hpp: In instantiation of 'struct boost::mpl::eval_if_c<false, boost::range_const_iterator<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>, boost::range_mutable_iterator<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())> >':
/Users/astacy/boost/include/boost/range/iterator.hpp:63:63: required from 'struct boost::range_iterator<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>'
/Users/astacy/boost/include/boost/range/join.hpp:34:7: required from 'class boost::range_detail::joined_type<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>'
/Users/astacy/boost/include/boost/range/join.hpp:43:7: required from 'class boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>'
src/test.cpp:22:49: required from here
/Users/astacy/boost/include/boost/mpl/eval_if.hpp:60:31: error: no type named 'type' in 'boost::mpl::eval_if_c<false, boost::range_const_iterator<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>, boost::range_mutable_iterator<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())> >::f_ {aka struct boost::range_mutable_iterator<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>}'
typedef typename f_::type type;
^
/Users/astacy/boost/include/boost/mpl/eval_if.hpp: In instantiation of 'struct boost::mpl::eval_if_c<true, boost::range_const_iterator<boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())> >, boost::range_mutable_iterator<const boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())> > >':
/Users/astacy/boost/include/boost/range/iterator.hpp:63:63: required from 'struct boost::range_iterator<const boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())> >'
/Users/astacy/boost/include/boost/range/begin.hpp:112:61: required by substitution of 'template<class T> typename boost::range_iterator<const T>::type boost::range_adl_barrier::begin(const T&) [with T = boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>]'
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp:58:64: required from 'static IteratorT boost::iterator_range_detail::iterator_range_impl<IteratorT>::adl_begin(ForwardRange&) [with ForwardRange = const boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>; IteratorT = line]'
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp:189:45: required from 'boost::iterator_range<IteratorT>::iterator_range(const Range&) [with Range = boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>; IteratorT = line]'
src/test.cpp:22:49: required from here
/Users/astacy/boost/include/boost/mpl/eval_if.hpp:60:31: error: no type named 'type' in 'boost::mpl::eval_if_c<true, boost::range_const_iterator<boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())> >, boost::range_mutable_iterator<const boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())> > >::f_ {aka struct boost::range_const_iterator<boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())> >}'
In file included from /Users/astacy/boost/include/boost/range/iterator_range.hpp:13:0,
from /Users/astacy/boost/include/boost/range/join.hpp:17,
from src/test.cpp:6:
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp: In instantiation of 'static IteratorT boost::iterator_range_detail::iterator_range_impl<IteratorT>::adl_begin(ForwardRange&) [with ForwardRange = const boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>; IteratorT = line]':
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp:189:45: required from 'boost::iterator_range<IteratorT>::iterator_range(const Range&) [with Range = boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>; IteratorT = line]'
src/test.cpp:22:49: required from here
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp:58:64: error: no matching function for call to 'begin(const boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>&)'
return static_cast<IteratorT>( boost::begin( r ) );
^
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp:58:64: note: candidates are:
In file included from /Users/astacy/boost/include/boost/range/detail/join_iterator.hpp:21:0,
from /Users/astacy/boost/include/boost/range/join.hpp:15,
from src/test.cpp:6:
/Users/astacy/boost/include/boost/range/begin.hpp:101:55: note: template<class T> typename boost::range_iterator<C>::type boost::range_adl_barrier::begin(T&)
inline BOOST_DEDUCED_TYPENAME range_iterator<T>::type begin( T& r )
^
/Users/astacy/boost/include/boost/range/begin.hpp:101:55: note: template argument deduction/substitution failed:
/Users/astacy/boost/include/boost/range/begin.hpp:112:61: note: template<class T> typename boost::range_iterator<const T>::type boost::range_adl_barrier::begin(const T&)
inline BOOST_DEDUCED_TYPENAME range_iterator<const T>::type begin( const T& r )
^
/Users/astacy/boost/include/boost/range/begin.hpp:112:61: note: substitution of deduced template arguments resulted in errors seen above
In file included from /Users/astacy/boost/include/boost/range/iterator_range.hpp:13:0,
from /Users/astacy/boost/include/boost/range/join.hpp:17,
from src/test.cpp:6:
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp: In instantiation of 'static IteratorT boost::iterator_range_detail::iterator_range_impl<IteratorT>::adl_end(ForwardRange&) [with ForwardRange = const boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>; IteratorT = line]':
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp:189:74: required from 'boost::iterator_range<IteratorT>::iterator_range(const Range&) [with Range = boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>; IteratorT = line]'
src/test.cpp:22:49: required from here
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp:64:62: error: no matching function for call to 'end(const boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>&)'
return static_cast<IteratorT>( boost::end( r ) );
^
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp:64:62: note: candidates are:
In file included from /Users/astacy/boost/include/boost/range/detail/join_iterator.hpp:22:0,
from /Users/astacy/boost/include/boost/range/join.hpp:15,
from src/test.cpp:6:
/Users/astacy/boost/include/boost/range/end.hpp:95:55: note: template<class T> typename boost::range_iterator<C>::type boost::range_adl_barrier::end(T&)
inline BOOST_DEDUCED_TYPENAME range_iterator<T>::type end( T& r )
^
/Users/astacy/boost/include/boost/range/end.hpp:95:55: note: template argument deduction/substitution failed:
/Users/astacy/boost/include/boost/range/end.hpp:106:61: note: template<class T> typename boost::range_iterator<const T>::type boost::range_adl_barrier::end(const T&)
inline BOOST_DEDUCED_TYPENAME range_iterator<const T>::type end( const T& r )
^
/Users/astacy/boost/include/boost/range/end.hpp:106:61: note: template argument deduction/substitution failed:
In file included from /Users/astacy/boost/include/boost/range/iterator_range.hpp:13:0,
from /Users/astacy/boost/include/boost/range/join.hpp:17,
from src/test.cpp:6:
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp: In static member function 'static IteratorT boost::iterator_range_detail::iterator_range_impl<IteratorT>::adl_begin(ForwardRange&) [with ForwardRange = const boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>; IteratorT = line]':
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp:59:13: error: control reaches end of non-void function [-Werror=return-type]
}
^
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp: In static member function 'static IteratorT boost::iterator_range_detail::iterator_range_impl<IteratorT>::adl_end(ForwardRange&) [with ForwardRange = const boost::range::joined_range<boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)()), boost::iterator_range<line>(std::istream_iterator<line>, std::istream_iterator<line> (*)())>; IteratorT = line]':
/Users/astacy/boost/include/boost/range/iterator_range_core.hpp:65:13: error: control reaches end of non-void function [-Werror=return-type]
}
^
In file included from /Users/astacy/boost/include/boost/filesystem/path_traits.hpp:23:0,
from /Users/astacy/boost/include/boost/filesystem/path.hpp:25,
from /Users/astacy/boost/include/boost/filesystem.hpp:16,
from src/util.hpp:7,
from src/test.cpp:8:
/Users/astacy/boost/include/boost/system/error_code.hpp: At global scope:
/Users/astacy/boost/include/boost/system/error_code.hpp:222:36: error: 'boost::system::posix_category' defined but not used [-Werror=unused-variable]
static const error_category & posix_category = generic_category();
^
/Users/astacy/boost/include/boost/system/error_code.hpp:223:36: error: 'boost::system::errno_ecat' defined but not used [-Werror=unused-variable]
static const error_category & errno_ecat = generic_category();
^
/Users/astacy/boost/include/boost/system/error_code.hpp:224:36: error: 'boost::system::native_ecat' defined but not used [-Werror=unused-variable]
static const error_category & native_ecat = system_category();
^
cc1plus: all warnings being treated as errors
make: *** [obj/test.o] Error 1