【发布时间】:2012-11-11 03:24:07
【问题描述】:
我有一个 boost::ptr_vector 包含指向“可持有”类的指针。
boost::ptr_vector<holdable> items;
我从可持有类中向该向量添加新项目,如下所示:
currentplanet->items.push_back(this);
其中 currentplanet 是指向包含 ptr_vector 的类的对象的指针。这一切都很好。
我感到困惑的是如何从 ptr_vector 中从它自己的类中的函数中删除一个条目。我正在尝试:
currentplanet->items.erase(std::find(currentplanet->items.begin(),
currentplanet->items.end(),
this));
根据此处类似问题的答案:How to erase elements from boost::ptr_vector,但我显然在某个地方出错了,可能与“this”的使用有关。
在尝试编译时,我收到来自 stl_algo.h 的错误提示
stl_algo.h|174|error: no match for 'operator==' in '__first.boost::void_ptr_iterator<VoidIter, T>::operator*
[with VoidIter = __gnu_cxx::__normal_iterator<void**, std::vector<void*, std::allocator<void*> > >,
T = holdable]() == __val'|
我确定这很明显,但我可能对 ptr_vector 的间接性感到困惑......提前感谢您的任何回答!
【问题讨论】:
-
你的设计无论如何都是有缺陷的,我认为。
ptr_vector拥有其中的指针。如果您已经有一个this,则 else 拥有该对象。将其添加到ptr_vector是没有意义的。只需使用std::vector<holdable*>对所有事物进行非拥有引用即可。
标签: c++ boost ptr-vector