【问题标题】:std::unique_ptr cannot be referenced -- it is a deleted functionstd::unique_ptr 不能被引用——它是一个被删除的函数
【发布时间】:2014-05-09 18:46:35
【问题描述】:

我目前正在努力将原始点转换为std::unique_ptr。我唯一不明白的是为什么这段代码不起作用:

auto it = entities.begin();
while (it != entities.end()) {
    int index = getIndex((*it)->getAABB());
    if (index != -1) {
        children.at(index)->insert(std::move(*it)); // Error
        it = entities.erase(it);
    }
    else {
        it++;
    }
}

实体是:

typedef std::unordered_set<std::unique_ptr<QuadTreeObject>> EntityContainer;
EntityContainer entities;

错误提示std::unique_ptr cannot be referenced, it is a deleted function.

【问题讨论】:

  • 你确定这是整个错误信息吗?似乎不完整。
  • 我怀疑这与std::unique_ptr 可移动但不可复制这一事实有关。
  • children是什么类型?

标签: c++ pointers c++11 smart-pointers unique-ptr


【解决方案1】:

您不能将unique_ptrs 存储在unordered_set 中并再次取出它们。

在容器中的访问是const,所以你的move什么都不做。

缺乏透明的哈希函数也使得查找元素变得困难。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-09
    • 2022-11-12
    • 2018-06-09
    • 1970-01-01
    • 2019-11-30
    • 2015-08-15
    • 2017-08-31
    • 2022-01-07
    相关资源
    最近更新 更多