problem passing const_iterator to a function expecting iterator

M

magnus.moraberg

Hi,

Im using the class MField<Type> from the H3D API -

http://www.h3dapi.org/uploads/api/H3DAPI_20/docs/H3DAPI/html/classH3D_1_1MField.html

which contains -

typedef vector< Type >::iterator iterator; // and

void insert(iterator pos, const Type &x, int id=0);

I wish to insert a value at the start of an object of this type, as
follows -

segmentField->insert(segmentField->begin(),10);

but the problem is that begin() gives me a const_iterator which I cant
pass to an iterator. What should I do?

Thanks,

Barry
 
J

joecook

Hi,

Im using the class MField<Type> from the H3D API -

http://www.h3dapi.org/uploads/api/H3DAPI_20/docs/H3DAPI/html/classH3D...

which contains -

typedef vector< Type >::iterator  iterator; // and

void insert(iterator pos, const Type &x, int id=0);

I wish to insert a value at the start of an object of this type, as
follows -

segmentField->insert(segmentField->begin(),10);

but the problem is that begin() gives me a const_iterator which I cant
pass to an iterator. What should I do?

Thanks,

Barry

I don't know what type "segmentField" is, but if it is a standard type
(vector/map), begin() will only return a const_iterator if the object
itself is const (a const vector for example). If that is the case,
you certainly cannot insert an element into a const vector.

Joe Cook
 
M

magnus.moraberg

Hi,

Im using the class MField<Type> from the H3D API -

http://www.h3dapi.org/uploads/api/H3DAPI_20/docs/H3DAPI/html/classH3D...

which contains -

typedef vector< Type >::iterator  iterator; // and

void insert(iterator pos, const Type &x, int id=0);

I wish to insert a value at the start of an object of this type, as
follows -

segmentField->insert(segmentField->begin(),10);

but the problem is that begin() gives me a const_iterator which I cant
pass to an iterator. What should I do?

Thanks,

Barry

Here's my code -


MFUnsignedInt8::const_iterator segmentIdFieldIterator; //
MField<unsigned char>
segmentIdFieldIterator = segmentIdField->begin();

MFUnsignedInt8::iterator x;// = segmentIdFieldIterator; // works if I
pass iterator, but not const_iterator
segmentIdField->insert(segmentIdFieldIterator,AIR_SEGMENT_ID);

Error message -

error: no matching function for call to ‘MFUnsignedInt8::insert
(__gnu_cxx::__normal_iterator<const unsigned char*,
std::vector<unsigned char, std::allocator<unsigned char> > >&, int)’
/usr/local/include/H3D/MField.h:446: note: candidates are: typename
std::vector<PtrType, std::allocator<_CharT> >::iterator
H3D::MField<Type>::insert(typename std::vector<PtrType,
std::allocator<_CharT> >::iterator, const Type&, int) [with Type =
unsigned char]
/usr/local/include/H3D/MField.h:468: note: void
H3D::MField<Type>::insert(typename std::vector<PtrType,
std::allocator<_CharT> >::iterator, typename H3D::MFieldBase<Type,
std::vector<PtrType, std::allocator<_CharT> >,
H3D::parsableMField>::size_type, const Type&, int) [with Type =
unsigned char]
make
 
Z

Zeppe

Hi,

Im using the class MField<Type> from the H3D API -

http://www.h3dapi.org/uploads/api/H3DAPI_20/docs/H3DAPI/html/classH3D_1_1MField.html

which contains -

typedef vector< Type >::iterator iterator; // and

void insert(iterator pos, const Type &x, int id=0);

I wish to insert a value at the start of an object of this type, as
follows -

segmentField->insert(segmentField->begin(),10);

but the problem is that begin() gives me a const_iterator which I cant
pass to an iterator. What should I do?

if begin returns an iterator rather than a const_iterator, it's likely
that your segmentField is const, so that you cannot modify it (you
cannot use the "insert" method).

Most likely, either you can have a non-const object to apply that method
to, or you may have a design problem in your code...

Best wishes,

Zeppe
 
M

magnus.moraberg

I don't know what type "segmentField" is, but if it is a standard type
(vector/map), begin() will only return a const_iterator if the object
itself is const (a const vector for example).  If that is the case,
you certainly cannot insert an element into a const vector.

Joe Cook

SegementField is declared as follows -

auto_ptr< MFUnsignedInt8 > segmentIdField;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,235
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top