Compiler error instantiating iterator for vector of pointers

Discussion in 'C++' started by cayblood, Nov 3, 2005.

  1. cayblood

    cayblood Guest

    Hello,

    I have a vector of pointers to a class, like so:

    vector<Node*> m_parents;

    I'm having trouble creating an iterator to go through the elements in
    this vector. Here is my syntax:

    vector<Node*>::iterator iter = m_parents.begin();

    And here is the error I'm getting:

    /usr/include/gcc/darwin/4.0/c++/bits/stl_iterator.h: In constructor
    `__gnu_cxx::__normal_iterator<_Iterator,
    _Container>::__normal_iterator(const
    __gnu_cxx::__normal_iterator<_Iter, _Container>&) [with _Iter =
    sbn::Node* const*, _Iterator = sbn::Node**, _Container =
    std::vector<sbn::Node*, std::allocator<sbn::Node*> >]':
    src/node.cpp:86: instantiated from here
    /usr/include/gcc/darwin/4.0/c++/bits/stl_iterator.h:609: error: invalid
    conversion from 'sbn::Node* const* const' to 'sbn::Node**'

    Any ideas?

    Thanks for all the help you guys have been giving me.

    Carl
     
    cayblood, Nov 3, 2005
    #1
    1. Advertising

  2. cayblood wrote:
    > Hello,
    >
    > I have a vector of pointers to a class, like so:
    >
    > vector<Node*> m_parents;
    >
    > I'm having trouble creating an iterator to go through the elements in
    > this vector. Here is my syntax:
    >
    > vector<Node*>::iterator iter = m_parents.begin();
    >
    > And here is the error I'm getting:
    >
    > /usr/include/gcc/darwin/4.0/c++/bits/stl_iterator.h: In constructor
    > `__gnu_cxx::__normal_iterator<_Iterator,
    > _Container>::__normal_iterator(const
    > __gnu_cxx::__normal_iterator<_Iter, _Container>&) [with _Iter =
    > sbn::Node* const*, _Iterator = sbn::Node**, _Container =
    > std::vector<sbn::Node*, std::allocator<sbn::Node*> >]':
    > src/node.cpp:86: instantiated from here
    > /usr/include/gcc/darwin/4.0/c++/bits/stl_iterator.h:609: error: invalid
    > conversion from 'sbn::Node* const* const' to 'sbn::Node**'
    >
    > Any ideas?
    >
    > Thanks for all the help you guys have been giving me.
    >
    > Carl
    >


    I would guess that you are calling this from within a const method.
    Therefore you must use a const_iterator.

    vector<Node*>::const_iterator iter = m_parents.begin();

    If that's not it then quote a bit more of the code. Preferably enough so
    that someone reading your post can actually compile the code and
    reproduce the error.

    john
     
    John Harrison, Nov 3, 2005
    #2
    1. Advertising

  3. cayblood

    cayblood Guest

    I figured it out. Don't know why, but the method I'm calling this in
    is a const method. It doesn't like the call to m_parents.begin(). I
    thought it wasn't changing anything in the vector state, but apparently
    it is.

    Thanks,
    Carl
     
    cayblood, Nov 3, 2005
    #3
  4. cayblood wrote:
    > I figured it out. Don't know why, but the method I'm calling this in
    > is a const method. It doesn't like the call to m_parents.begin(). I
    > thought it wasn't changing anything in the vector state, but apparently
    > it is.
    >
    > Thanks,
    > Carl
    >


    Because you are calling in a const method, the version of begin that you
    are calling returns a const_iterator. It is not allowed to assign a
    const_iterator to an iterator for obvious reasons.

    john
     
    John Harrison, Nov 3, 2005
    #4
  5. cayblood

    cayblood Guest

    I figured it out. Don't know why, but the method I'm calling this in
    is a const method. It doesn't like the call to m_parents.begin(). I
    thought it wasn't changing anything in the vector state, but apparently
    it is.

    Thanks,
    Carl
     
    cayblood, Nov 3, 2005
    #5
  6. cayblood

    cayblood Guest

    I figured it out. Don't know why, but the method I'm calling this in
    is a const method. It doesn't like the call to m_parents.begin(). I
    thought it wasn't changing anything in the vector state, but apparently
    it is.

    Thanks,
    Carl
     
    cayblood, Nov 3, 2005
    #6
    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. Ken Cecka
    Replies:
    3
    Views:
    7,384
    Ken Cecka
    Mar 27, 2005
  2. Replies:
    2
    Views:
    931
  3. Replies:
    8
    Views:
    2,007
    Csaba
    Feb 18, 2006
  4. zl2k
    Replies:
    27
    Views:
    1,662
    Francesco S. Carta
    Sep 7, 2010
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    739
Loading...

Share This Page