Compiler error instantiating iterator for vector of pointers

C

cayblood

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
 
J

John Harrison

cayblood said:
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
 
C

cayblood

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
 
J

John Harrison

cayblood said:
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
 
C

cayblood

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
 
C

cayblood

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
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top