C++ 遍历智能指针 vector

标签 c++ stl vector smart-pointers

我有一个具有此功能的类:

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

    }
}

编译器告诉我类 boost::shared_ptr 没有名为“RenderShape”的成员,自从 PrimShapeBase 类当然具有该功能,但位于不同的头文件中。 这是什么原因?

最佳答案

你不是说

(*i)->RenderShape(destination); 

?

i 是迭代器,*ishared_ptr(*i​​)::operator->() 是对象。

关于C++ 遍历智能指针 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11960631/

相关文章:

C++:读取数据集并检查 vector<Class> 是否是 vector<Class> 的子集

c++ - 将消息片段连接在一起

C++在编译时在两个变量之间交替

c++ - STL list<mystruct>返回问题

c++ - ifstream、ofstream 和 fstream 之间有什么区别?

c++ - 从控制台读取字符

编译器中的 C++ 迭代与递归优化

c++ - 将 future 存储在列表中

c++ - 为什么使用 `std::reverse_iterator` 不调用 UB?

c++ - 使用 std::vector 和类对象时出错