Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Iterating through a vector of smart pointers

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 class boost::shared_ptr has no member called 'RenderShape' which I find bizarre since the class PrimShapeBase certainly has that function but is in a different header file. What is the cause of this?

like image 790
lambda Avatar asked Aug 14 '12 20:08

lambda


2 Answers

Don't you mean

(*i)->RenderShape(destination); 

?

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

like image 144
Luchian Grigore Avatar answered Nov 05 '22 21:11

Luchian Grigore


That's because i is an iterator. Dereferencing it once gives you the smart pointer, you need to double dereference it.

(**i).RenderShape(destination);

or

(*i)->RenderShape(destination); 
like image 21
Wug Avatar answered Nov 05 '22 20:11

Wug