unique_ptr列表的内容不可访问
Contents of list of unique_ptr are inaccessible
我有一个std::unique_ptrs
到Entity
对象的std::list
。当我尝试这样循环它们时,程序说列表中的项是不可访问的。list是一个成员变量,声明为private:unique_ptr> .
void EntityContainer::E_Update(int delta)
{
for (auto& child : children)
child->Update(delta);
}
其中Update()
为Entity的公共函数。但是,在编译时,我得到以下错误:
c:program files (x86)microsoft visual studio 11.0vcincludexmemory0(617): error C2248:
'std::unique_ptr<_Ty>::unique_ptr'
: cannot access private member in class'std::unique_ptr<_Ty>'
您正在尝试复制unique_ptr
。不能复制,只能移动。
第一种情况,使用引用:
for (auto const & child : children) {
child->Update(delta);
}
在第二种情况下,直接使用解引用迭代器:
for (auto child = children.begin(); child != children.end(); ++child) {
(*child)->Render();
}
或者,如果你真的需要一个单独的变量,让它成为一个引用:
unique_ptr<Entity> const & childPtr = *child;
我知道有一种新形式的基于范围的for
循环的建议,它将通过引用访问元素:
for (child : children) {
child->Update(delta);
}
但是官方还不存在
相关文章:
- 通过方法访问结构
- 使用不带参数的函数访问结构元素
- 如果我只是不访问queue_front节点的子节点,而是将它们推到队列中呢?还是BFS吗
- 用于访问容器<T>数据成员的正确 API
- 访问者访问变体并返回不同类型时出错
- 尝试通过多个向量访问变量时,向量下标超出范围
- 无法访问嵌套类.类的使用无效
- 写入位置0x0000000C时发生访问冲突
- 我们可以访问一个不存在的联盟的成员吗
- C++从另一个类访问公共静态向量的正确方法是什么
- 我的简单if-else语句是如何无法访问的代码
- 从C++dll访问C#中的一行主要参数
- 概念TS检查忽略私有访问修饰符
- 访问被拒绝后,c++中的故障保护代码
- 如何从OpenCV Mat访问浮点值(.at和.ptr除外)
- 读取访问冲突_First为空Ptr
- 如何访问受保护的基类函数,从派生类通过基类ptr
- 从数组的共享ptr访问共享ptr
- 为什么int** ptr在访问ptr[i][j]时不指向与int arr[3][3]相同的地址?
- 当可以在类对象超出范围之前删除此数据时,类允许访问其数据(通过 ptr/it)是否设计不好