Wednesday, 15 January 2014

iterator - How to copy a const_iterator in C++ -



iterator - How to copy a const_iterator in C++ -

in header file have

template <typename t> class vector { public: typedef t* iterator; typedef const t* const_iterator; vector(const_iterator start, const_iterator end); // other stuff ... }

and in .cpp file have

template <typename t> vector< t >::vector( const_iterator start, const_iterator end ) : thesize( 0 ), thecapacity( 1 ) { array = new t[ thecapacity ]; typename vector< t >::iterator itr = start; // line 29 for( iterator itr = start; itr != end; itr++ ){ push_back( *itr ); } }

however compiler telling me

vector.cpp:29: error: invalid conversion ‘const int*’ ‘int*’

how can around problem while keeping parameters const_iterator?

note: if helps i'm trying build vector elements vector between start , end.

i not see reason here not using const_iterator in loop well. also, push_back function should not take iterator, value.

thus, rewrite relevant part follows:

typename vector< t >::const_iterator itr = start; for(const_iterator itr = start; itr != end; itr++ ){ push_back(*itr); }

c++ iterator

No comments:

Post a Comment