且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

错误C2280:尝试引用已删除的函数(unique_ptr)

更新时间:2022-02-26 08:44:07

unique_ptr s无法复制;只感动!但是由于 std :: list 应该能够在内部移动它们,所以唯一的问题应该是您要对列表本身执行分配。

unique_ptrs cannot be copied; only moved! But since std::list should be able to move them around internally, your only problem should be that assignment you're performing to the list itself.

可以移动列表吗?


  • std :: list< PEnemy>敌人= std :: move(Game :: LEVEL-> getEnemies());

  • std::list<PEnemy> enemies = std::move(Game::LEVEL->getEnemies());


  • const std :: list< PEnemy>&敌人= Game :: LEVEL-> getEnemies();

  • const std::list<PEnemy>& enemies = Game::LEVEL->getEnemies();

如果没有(这取决于在 Game :: LEVEL-> getEnemies()的返回类型上,您将需要使用上述哪种解决方案,如果有的话,进行深层复制,或者将深层复制切换到 shared_ptr

If not (and it will depend on the return type of Game::LEVEL->getEnemies() as to which, if either, of the above solutions you can use), you're going to need to do a deep, deep copy or switch to shared_ptr instead.

这全部可能似乎是一个障碍,但这实际上是通过严格执行有关指尖所有者所有权的规则来帮您一个忙。

This all may seem to be a hindrance, but it's actually doing you a favour by keeping the rules regarding ownership of your pointees strictly enforced.