I am trying to write a simple STL iterator for CArray MFC class using boost iterator adaptor. This is my code:
#include <boost/iterator/iterator_adaptor.hpp>
#include <afxtempl.h>
class CArrIter : public boost::iterator_adaptor< CArrIter ,
int,
int,
boost::random_access_traversal_tag >
{
public:
CArrIter(CArray<int,int>& arr, int index = 0) : m_arr(arr)
{
this->base_reference() = index;
}
private:
friend class boost::iterator_core_access;
int dereference() const{
return m_arr.GetAt(base());
}
private:
CArray<int,int>& m_arr;
};
This compiles fine with VC9 compiler. But when I try compiling this with VC7 I get the following error:
\include\boost\iterator\iterator_traits.hpp(49) : erro r C2039: 'difference_type' : is not a member of 'boost::detail::iterator_traits< Iterator>' with [ Iterator=int ]
\开发者_C百科include\boost\mpl\eval_if.hpp(41) : see refer ence to class template instantiation 'boost::iterator_difference' bein g compiled with [ Iterator=int ]
.... Some more ....
Any clues what could be wrong? I have to include some other header files? I am quite new to boost library.
I think that the second template parameter of boost::iterator_adaptor<> has to be a valid iterator type, try using int* instead of int.
It could be related to the random access behavior not having everything it needs to traverse the container. The 'iterator_adaptor requirements' section of this link might help:
Boost: Iterator Adapter
I'm not sure if int is assignable, so I wonder what would happen if you changed int to int&.
A couple more ideas:
- Are you using the same version of the Boost library with both compilers?
- Does making dereference() protected or public help?
精彩评论