problem passing const_iterator to a function expecting iterator

Discussion in 'C++' started by magnus.moraberg@gmail.com, Feb 23, 2009.

  1. Guest

    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
     
    , Feb 23, 2009
    #1
    1. Advertising

  2. Guest

    On Feb 23, 11:13 am, wrote:
    > 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
     
    , Feb 23, 2009
    #2
    1. Advertising

  3. Guest

    On 23 Feb, 17:13, wrote:
    > 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
     
    , Feb 23, 2009
    #3
  4. Zeppe Guest

    wrote [23/02/09 16:13]:
    > 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
     
    Zeppe, Feb 23, 2009
    #4
  5. Guest

    On 23 Feb, 17:23, wrote:
    > On Feb 23, 11:13 am, wrote:
    >
    >
    >
    > > 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


    SegementField is declared as follows -

    auto_ptr< MFUnsignedInt8 > segmentIdField;
     
    , Feb 23, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CoolPint
    Replies:
    3
    Views:
    748
    CoolPint
    Dec 13, 2003
  2. Replies:
    2
    Views:
    376
    Ferdi Smit
    Oct 23, 2005
  3. John Harrison

    iterator and const_iterator

    John Harrison, Nov 20, 2005, in forum: C++
    Replies:
    5
    Views:
    1,309
    Cy Edmunds
    Nov 20, 2005
  4. Mark P
    Replies:
    2
    Views:
    738
    Bart van Ingen Schenau
    Jun 15, 2006
  5. flopbucket
    Replies:
    1
    Views:
    720
    flopbucket
    Jun 21, 2006
Loading...

Share This Page