Boost logo

Boost Users :

From: Marshall Clow (marshall_at_[hidden])
Date: 2007-03-05 12:23:48


At 5:28 PM +0100 3/5/07, Vaclav wrote:
>Hi all,
>
>Can you please tell me what is wrong with this? I do not know how to iterate
>thru a vector. The error is shown on the last line.
>
>
>
> typedef boost::shared_ptr<MySillyClass> mySharedPtr;
> std::vector<mySharedPtr> vec;
>
> vec.push_back( mySharedPtr(new MySillyClass("bigString")) );
> vec.push_back( mySharedPtr(new MySillyClass("smallOne")) );
> vec.push_back( mySharedPtr(new MySillyClass("anotherOne")) );
> vec.push_back( mySharedPtr(new MySillyClass("bullShit")) );
>
> mySharedPtr ptr2 = vec[2];
>
> if(ptr2.use_count() > 0)
> {
> ptr2->DoSomething(); // <------- works OK
> }
>
> for (std::vector<mySharedPtr>::iterator i = vec.begin(); i != vec.end();
>++i)
> {
> i->DoSomething(); // <------- gives error "DoSomething' : is not a
>member of 'shared_ptr<class MySillyClass>"
>
> }

i-> refers to the thing that the iterator points to, i.e, the
shared pointer.
So, "i->DoSomething()" would correspond to "ptr2.DoSomething ()" in
your first example.

What you want, I think, is:
        (*i)->DoSomething();

-- 
-- Marshall
Marshall Clow     Idio Software   <mailto:marshall_at_[hidden]>
It is by caffeine alone I set my mind in motion.
It is by the beans of Java that thoughts acquire speed,
the hands acquire shaking, the shaking becomes a warning.
It is by caffeine alone I set my mind in motion.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net