C ++ Iterating through a Smart Pointer Vector

I have a class that has this function:

typedef boost::shared_ptr<PrimShapeBase> sp_PrimShapeBase; class Control{ public: //other functions RenderVectors(SDL_Surface*destination, sp_PrimShapeBase); private: //other vars vector<sp_PrimShapeBase> LineVector; }; //the problem of the program void Control::RenderVectors(SDL_Surface*destination, sp_PrimShapeBase){ vector<sp_PrimShapeBase>::iterator i; //iterate through the vector for(i = LineVector.begin(); i != LineVector.end(); i ++ ){ //access a certain function of the class PrimShapeBase through the smart //pointers (i)->RenderShape(destination); } } 

The compiler tells me that the boost :: shared_ptr class does not have a member called "RenderShape", which I find strange, since the PrimShapeBase class certainly has this function, but is in a different header file. What is the reason for this?

+7
source share
2 answers

Don't you mean

 (*i)->RenderShape(destination); 

?

i is an iterator, *i is shared_ptr , (*i)::operator->() is an object.

+16
source

This is because i is an iterator. Deploying it once gives you a smart pointer, you need to double its playing.

 (**i).RenderShape(destination); 

or

 (*i)->RenderShape(destination); 
+5
source

All Articles