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

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@idio.com 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.
participants (2)
-
Marshall Clow
-
Vaclav