C++ 遍历智能指针向量
C++ Iterating through a vector of 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没有名为"渲染形状"的成员,我觉得很奇怪,因为类 PrimShapeBase 当然具有该函数,但位于不同的头文件中。这是什么原因造成的?
你不是说
(*i)->RenderShape(destination);
?
i
是迭代器,*i
是shared_ptr
,(*i)::operator->()
是对象。
那是因为i
是一个迭代器。 取消引用它一次会给你智能指针,你需要双重取消引用它。
(**i).RenderShape(destination);
或
(*i)->RenderShape(destination);
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- 有没有一种"cleaner"的方法可以在指向基的指针向量中找到派生类的第一个实例?
- 使用向量类的c++指针
- 如何使用C++初始化向量;脚本化值不是数组、指针或矢量错误
- 如何更改唯一指针向量的可见性
- C++ 独特指针练习的向量
- C 中的向量指针的清晰图
- 当向量位于内部类中时,如何删除指向对象的向量指针
- std::向量指针
- 指向结构体的向量指针错误
- 删除指向指针的向量指针时出错
- 映射不同类型的向量指针
- 在c++中传递向量指针
- 虚拟类的向量:指针是干净的方法吗
- 如何从另一个向量/对象集构造新的向量/指针集
- 模型视图控制器- c++向量指针,太多的传递
- 为什么向量指针占用这么多内存?
- 删除条目向量指针的静态映射c++
- 复向量指针
- c++数组向量指针的初始化