【发布时间】:2018-12-06 10:20:22
【问题描述】:
我有以下数据结构:
shared_ptr<vector<shared_ptr<Drawable>>> foo;
还有一个带有函数的 Renderer 类:
void addObject(weak_ptr<T> _obj) const
这个函数只是将_obj推回
mutable vector<weak_ptr<T>> m_objects;
当我尝试以下操作时:
Renderer r;
for(auto& d: *foo) {
r.addObject(d);
}
GCC 5.1 出现以下错误:
error: no match for 'operator==' (operand types are 'std::weak_ptr<Drawable>' and 'const std::weak_ptr<Drawable>')|
我不明白 const 是从哪里来的。
foo
无论如何都不是 const,并且 addObject 不采用 const weak_ptr。
编辑:我觉得我太小了。 addObject 的内容如下:
void addObject(std::weak_ptr<T> _obj) const {
auto it = std::find(m_objects.begin(), m_objects.end(), _obj);
if(it == m_objects.end()) {
m_objects.push_back(_obj);
}
};
如果我注释掉除实际 push_back 行之外的所有内容,它会起作用。我猜迭代器将自己作为迭代器分配给 const weak_ptr。如果它已经存在,我只是想避免将它添加到向量中。
【问题讨论】:
-
无法复制。请提供minimal reproducible example
-
不清楚 == 来自哪里。可以发minimal reproducible example吗?
-
发生错误的
addObject里面是什么? -
抱歉太小了。我已经编辑了 OP。
-
还没有完成。制作minimal reproducible example。实事求是。
标签: c++ gcc vector shared-ptr weak-ptr