Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > problem passing const_iterator to a function expecting iterator

Reply
Thread Tools

problem passing const_iterator to a function expecting iterator

 
 
magnus.moraberg@gmail.com
Guest
Posts: n/a
 
      02-23-2009
Hi,

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

http://www.h3dapi.org/uploads/api/H3...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

 
Reply With Quote
 
 
 
 
joecook@gmail.com
Guest
Posts: n/a
 
      02-23-2009
On Feb 23, 11:13*am, (E-Mail Removed) wrote:
> Hi,
>
> Im using the class MField<Type> from the H3D API -
>
> http://www.h3dapi.org/uploads/api/H3.../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
 
Reply With Quote
 
 
 
 
magnus.moraberg@gmail.com
Guest
Posts: n/a
 
      02-23-2009
On 23 Feb, 17:13, (E-Mail Removed) wrote:
> Hi,
>
> Im using the class MField<Type> from the H3D API -
>
> http://www.h3dapi.org/uploads/api/H3.../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:arsableMField>::size_type, const Type&, int) [with Type =
unsigned char]
make
 
Reply With Quote
 
Zeppe
Guest
Posts: n/a
 
      02-23-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote [23/02/09 16:13]:
> Hi,
>
> Im using the class MField<Type> from the H3D API -
>
> http://www.h3dapi.org/uploads/api/H3...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
 
Reply With Quote
 
magnus.moraberg@gmail.com
Guest
Posts: n/a
 
      02-23-2009
On 23 Feb, 17:23, (E-Mail Removed) wrote:
> On Feb 23, 11:13*am, (E-Mail Removed) wrote:
>
>
>
> > Hi,

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

>
> >http://www.h3dapi.org/uploads/api/H3.../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


SegementField is declared as follows -

auto_ptr< MFUnsignedInt8 > segmentIdField;
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
implementing const_iterator and iterator flopbucket C++ 1 06-21-2006 08:49 PM
comparing iterator with const_iterator Mark P C++ 2 06-15-2006 06:21 PM
iterator and const_iterator John Harrison C++ 5 11-20-2005 11:41 PM
How the comparison operators are defined for iterator and const_iterator PengYu.UT@gmail.com C++ 2 10-23-2005 04:33 PM
Questions about Getting const_iterator out of iterator class CoolPint C++ 3 12-13-2003 04:13 AM



Advertisments