Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Compiler error instantiating iterator for vector of pointers

Reply
Thread Tools

Compiler error instantiating iterator for vector of pointers

 
 
cayblood
Guest
Posts: n/a
 
      11-03-2005
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

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      11-03-2005
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
 
Reply With Quote
 
 
 
 
cayblood
Guest
Posts: n/a
 
      11-03-2005
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

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      11-03-2005
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
 
Reply With Quote
 
cayblood
Guest
Posts: n/a
 
      11-03-2005
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

 
Reply With Quote
 
cayblood
Guest
Posts: n/a
 
      11-03-2005
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

 
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
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
can I use stl vector iterator to delete a vector of pointers? zl2k C++ 27 09-07-2010 11:47 AM
Problems instantiating a Vector<Vector<Integer>> the.real.doctor.zoidberg@gmail.com Java 2 11-01-2006 01:38 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
trying to declare an iterator for a std::vector of template pointers Ken Cecka C++ 3 03-27-2005 08:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57