pointer to const std::vector iterators 
Author Message
 pointer to const std::vector iterators

I'm having a problem using a <classname * const> vector iterator. Here's the
snippet of code that causes my compiler error (C2535):

    for (std::vector<MyClass * const>::iterator it = m_vector.begin(); ...

The compiler (msvc 6.0, sp4) generates an error for this code:

C:\Program Files\Microsoft Visual Studio\VC98\include\xmemory(57) : error
C2535:
 'class MyClass *const *__thiscall std::allocator<class MyClass *
const>::address(class MyClass *const & ) const ' : member function already
defined or declared
        C:\Program Files\Microsoft Visual Studio\VC98\include\xmemory(54) :
see declaration of 'address'
        C:\Program Files\Microsoft Visual Studio\VC98\include\vector(24) :
see reference to class template instantiation 'std::allocator<class MyClass
* const>' being compiled
        MyClass.cpp(38) : see reference to class template instantiation
'std::vector<class MyClass * const,class std::allocator<class MyClass *
const> >' being compiled

I don't even know where to start on this one. The member function this code
contains must be const-safe, yet using a const iterator on my vector causes
this error to occur.

Any ideas?
Thanks,
John



Sat, 04 Oct 2003 03:50:41 GMT  
 pointer to const std::vector iterators

Quote:
> I'm having a problem using a <classname * const> vector iterator. Here's
the
> snippet of code that causes my compiler error (C2535):

>     for (std::vector<MyClass * const>::iterator it = m_vector.begin(); ...

> The compiler (msvc 6.0, sp4) generates an error for this code:

> C:\Program Files\Microsoft Visual Studio\VC98\include\xmemory(57) : error
> C2535:
>  'class MyClass *const *__thiscall std::allocator<class MyClass *
> const>::address(class MyClass *const & ) const ' : member function already
> defined or declared
>         C:\Program Files\Microsoft Visual Studio\VC98\include\xmemory(54)
:
> see declaration of 'address'
>         C:\Program Files\Microsoft Visual Studio\VC98\include\vector(24) :
> see reference to class template instantiation 'std::allocator<class
MyClass
> * const>' being compiled
>         MyClass.cpp(38) : see reference to class template instantiation
> 'std::vector<class MyClass * const,class std::allocator<class MyClass *
> const> >' being compiled

> I don't even know where to start on this one. The member function this
code
> contains must be const-safe, yet using a const iterator on my vector
causes
> this error to occur.

In your code:

Quote:
>     for (std::vector<MyClass * const>::iterator it = m_vector.begin(); ...

you are _not_ creating a const iterator! A const iterator looks like this:

std::vector<...> :: const_iterator it;

whether or not this is the problem depends on the definition of m_vector,
which you haven't shown us.

NeilB



Sat, 04 Oct 2003 03:12:19 GMT  
 pointer to const std::vector iterators
This works, thanks very much! I told you I was new to C++ programming,
specifically STL programming. I didn't even realize there was a
"const_iterator" construct (this being a bit counter-intuitive and all :o)

Incidentally, the definition of m_vector is:

        std::vector<MyClass *> m_vector;

All I wanted was a const accessor so that my member routine would be const
object safe.

Thanks again for the advice,
John


Quote:


> > I'm having a problem using a <classname * const> vector iterator. Here's
> the
> > snippet of code that causes my compiler error (C2535):

> >     for (std::vector<MyClass * const>::iterator it = m_vector.begin();
...

> > The compiler (msvc 6.0, sp4) generates an error for this code:

> > C:\Program Files\Microsoft Visual Studio\VC98\include\xmemory(57) :
error
> > C2535:
> >  'class MyClass *const *__thiscall std::allocator<class MyClass *
> > const>::address(class MyClass *const & ) const ' : member function
already
> > defined or declared
> >         C:\Program Files\Microsoft Visual

Studio\VC98\include\xmemory(54)

- Show quoted text -

Quote:
> :
> > see declaration of 'address'
> >         C:\Program Files\Microsoft Visual Studio\VC98\include\vector(24)
:
> > see reference to class template instantiation 'std::allocator<class
> MyClass
> > * const>' being compiled
> >         MyClass.cpp(38) : see reference to class template instantiation
> > 'std::vector<class MyClass * const,class std::allocator<class MyClass *
> > const> >' being compiled

> > I don't even know where to start on this one. The member function this
> code
> > contains must be const-safe, yet using a const iterator on my vector
> causes
> > this error to occur.

> In your code:

> >     for (std::vector<MyClass * const>::iterator it = m_vector.begin();
...

> you are _not_ creating a const iterator! A const iterator looks like this:

> std::vector<...> :: const_iterator it;

> whether or not this is the problem depends on the definition of m_vector,
> which you haven't shown us.

> NeilB



Sat, 04 Oct 2003 04:21:42 GMT  
 pointer to const std::vector iterators
There is some benefit when you typedef your containers:

typedef std::vector<my_class*> my_classes;
my_classes m_my_classes;

for (my_classes::const_iterator mci = m_my_classes.begin (); mci !=
m_my_classes.end (); ++ mci)
{
    ...

Quote:
}

my_classes:size_type size = m_my_classes.size ();

and so on.  Makes it much easier to change container types as well.


Quote:
> This works, thanks very much! I told you I was new to C++ programming,
> specifically STL programming. I didn't even realize there was a
> "const_iterator" construct (this being a bit counter-intuitive and all :o)

> Incidentally, the definition of m_vector is:

>         std::vector<MyClass *> m_vector;

> All I wanted was a const accessor so that my member routine would be const
> object safe.

> Thanks again for the advice,
> John





> > > I'm having a problem using a <classname * const> vector iterator.
Here's
> > the
> > > snippet of code that causes my compiler error (C2535):

> > >     for (std::vector<MyClass * const>::iterator it = m_vector.begin();
> ...

> > > The compiler (msvc 6.0, sp4) generates an error for this code:

> > > C:\Program Files\Microsoft Visual Studio\VC98\include\xmemory(57) :
> error
> > > C2535:
> > >  'class MyClass *const *__thiscall std::allocator<class MyClass *
> > > const>::address(class MyClass *const & ) const ' : member function
> already
> > > defined or declared
> > >         C:\Program Files\Microsoft Visual
> Studio\VC98\include\xmemory(54)
> > :
> > > see declaration of 'address'
> > >         C:\Program Files\Microsoft Visual

Studio\VC98\include\vector(24)

- Show quoted text -

Quote:
> :
> > > see reference to class template instantiation 'std::allocator<class
> > MyClass
> > > * const>' being compiled
> > >         MyClass.cpp(38) : see reference to class template
instantiation
> > > 'std::vector<class MyClass * const,class std::allocator<class MyClass
*
> > > const> >' being compiled

> > > I don't even know where to start on this one. The member function this
> > code
> > > contains must be const-safe, yet using a const iterator on my vector
> > causes
> > > this error to occur.

> > In your code:

> > >     for (std::vector<MyClass * const>::iterator it = m_vector.begin();
> ...

> > you are _not_ creating a const iterator! A const iterator looks like
this:

> > std::vector<...> :: const_iterator it;

> > whether or not this is the problem depends on the definition of
m_vector,
> > which you haven't shown us.

> > NeilB



Sat, 04 Oct 2003 05:23:56 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. std::list, std::vector, push_back() and associated ::iterator validity

2. std::vector; 0 based index to iterator

3. using std::vector and iterator objects

4. Iterator and const Iterator?

5. VC++ 5.0 std::vector error: pointer to nested class

6. bug: VS7.0 (6.0) C++ std::auto_ptr conflict with std::vector

7. Warnings for std::vector<std::string>

8. export classes using std namespace (ex std::vector) in a DLL

9. Type Casting Const Pointers to non const pointers

10. typedef std::vector<std::string> Vector_String

11. Using find_if returns const iterator, but find returns non const iterator, why?

12. Problems with std::list<E>::iterator which requires that E is defines vc++

 

 
Powered by phpBB® Forum Software