how to iterate a vector?

Discussion in 'C++' started by vjedlicka@atlas.cz, Mar 5, 2007.

  1. Guest

    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.

    Thank you
    Vaclav


    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>"

    }
    , Mar 5, 2007
    #1
    1. Advertising

  2. David Harmon Guest

    On 5 Mar 2007 08:39:38 -0800 in comp.lang.c++, ""
    <> wrote,
    > 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 is the vec iterator.

    (*i) is the shared_ptr.

    You wanted: (*i)->DoSomething();
    David Harmon, Mar 5, 2007
    #2
    1. Advertising

  3. Guest

    On Mar 5, 12:54 pm, David Harmon <> wrote:
    > On 5 Mar 2007 08:39:38 -0800 in comp.lang.c++, ""
    > <> wrote,
    >
    > > 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 is the vec iterator.
    >
    > (*i) is the shared_ptr.
    >
    > You wanted: (*i)->DoSomething();


    Actually, if I read the Boost shared_ptr header correctly, it
    implements "T* shared_ptr<T>::eek:perator ->() const" and should be
    called as a matter of course. I know since I've made proxy objects
    that do this sort of thing before.

    Not sure. Let us know if (*i)->DoSomething(); works.


    Adrian
    , Mar 5, 2007
    #3
  4. Ron Natalie Guest

    wrote:

    > ptr2->DoSomething(); // <------- works OK


    ptr2 is of type mySharedPtr, which has an operator->

    > i->DoSomething(); // <------- gives error "DoSomething' : is
    > not a
    > member of 'shared_ptr<class MySillyClass>"


    i yields something that behaves as if it were mySharedPtr*
    which means you could do
    i->useCount()
    but not the mySharedPoint::eek:perator->

    You can do
    (*i)->DoSomething();
    Ron Natalie, Mar 5, 2007
    #4
  5. Ron Natalie Guest


    >
    > Actually, if I read the Boost shared_ptr header correctly, it
    > implements "T* shared_ptr<T>::eek:perator ->() const" and should be
    > called as a matter of course. I know since I've made proxy objects
    > that do this sort of thing before.
    >


    You're getting the confused over the things that look like pointers
    and their level of indirection.

    i behaves as if it were a point to a thing that behaves like
    a pointer.

    The vector<T>::eek:perator-> returns T* not T. T*. The T*::eek:perator->
    only gets you members of T, not the T::eek:perator->
    Ron Natalie, Mar 5, 2007
    #5
  6. Guest

    On Mar 5, 4:13 pm, Ron Natalie <> wrote:
    > wrote:
    > > ptr2->DoSomething(); // <------- works OK

    >
    > ptr2 is of type mySharedPtr, which has an operator->
    >
    > > i->DoSomething(); // <------- gives error "DoSomething' : is
    > > not a
    > > member of 'shared_ptr<class MySillyClass>"

    >
    > i yields something that behaves as if it were mySharedPtr*
    > which means you could do
    > i->useCount()
    > but not the mySharedPoint::eek:perator->
    >
    > You can do
    > (*i)->DoSomething();


    Your right. What I did was write something that did the indirection
    for it, like this:

    class A
    {
    int a;
    public:
    A(): a(5) {}
    void set(int newA) { a = newA; }
    int get() { return a; }
    };

    class B
    {
    A a;
    public:
    operator A*() { return &a; }
    };

    class C
    {
    B b;
    public:
    A* operator->() { return b; }
    };

    int main()
    {
    C c;
    c->set(3);
    cout << c->get() << endl;
    return 0;
    }

    The indirection that is happening with the pointer is like a pointer
    to a pointer, which requires that you dereference the pointer prior to
    using '->', or you double dereference and use '.'.

    i is of type shared_ptr<MySillyClass>
    *i is of type MySillyClass*
    **i is of type MySillyClass&

    (*i)->DoSomething();
    (**i).DoSomething();

    My mistake.


    Adrian
    , Mar 6, 2007
    #6
  7. Guest

    On Mar 5, 9:52 pm, wrote:
    > The indirection that is happening with the pointer is like a pointer
    > to a pointer, which requires that you dereference the pointer prior to
    > using '->', or you double dereference and use '.'.


    Er, what I ment to say was:

    The indirection that is happening with the *iterator* is like that of
    a pointer
    to a pointer, which requires that you dereference the pointer prior to
    using '->', or you double dereference and use '.'.


    Adrian
    , Mar 6, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gogo
    Replies:
    1
    Views:
    2,088
    Sudsy
    Sep 4, 2003
  2. runescience
    Replies:
    0
    Views:
    1,444
    runescience
    Feb 9, 2006
  3. pmatos
    Replies:
    6
    Views:
    23,739
  4. Replies:
    8
    Views:
    1,896
    Csaba
    Feb 18, 2006
  5. John
    Replies:
    4
    Views:
    898
    RedGrittyBrick
    Apr 1, 2008
Loading...

Share This Page