how to iterate a vector?

V

vjedlicka

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

}
 
D

David Harmon

On 5 Mar 2007 08:39:38 -0800 in comp.lang.c++, "(e-mail address removed)"
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();
 
A

adrian.hawryluk

On 5 Mar 2007 08:39:38 -0800 in comp.lang.c++, "(e-mail address removed)"



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
 
R

Ron Natalie

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();
 
R

Ron Natalie

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

adrian.hawryluk

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


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
 
A

adrian.hawryluk

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top