By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,377 Members | 1,660 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,377 IT Pros & Developers. It's quick & easy.

Compiler error instantiating iterator for vector of pointers

P: n/a
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

Nov 3 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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
Nov 3 '05 #2

P: n/a
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

Nov 3 '05 #3

P: n/a
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
Nov 3 '05 #4

P: n/a
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

Nov 3 '05 #5

P: n/a
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

Nov 3 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.