无法分配对象,因为其副本分配运算符被隐式删除错误 [英] Object cannot be assigned because its copy assignment operator is implicitly deleted error

查看:68
本文介绍了无法分配对象,因为其副本分配运算符被隐式删除错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的小型打砖块克隆游戏中,我试图从向量中删除一些值。此向量包含Brick类,这些类在屏幕上以网格状模式进行实例化。当球与砖块之间发生碰撞时,砖块需要消失。我正在尝试通过以下一小段代码来实现这一点:

In my small arkanoid clone game I'm trying to erase some values from a vector. This vector contains Brick classes, that are instantiated on the screen in a grid like pattern. When a collision happens between the ball and a brick, the brick needs to disappear. I'm trying to accomplish this with this small piece of code:

for (int i = 0; i < bricks.size(); ++i)
{
    if (bricks[i].destroyed)
    {
        bricks.erase(bricks.begin()+i);
    }
}

但不幸的是,我得到了这个编译错误:

But unfortunately I get this compile error:

Object of type 'Brick' cannot be assigned because its copy assignment operator is implicitly deleted

当我单击此错误时,它带我到这段代码:

When I click on this error it brings me to this piece of code:

for (; __first != __last; ++__first, (void) ++__result)
    *__result = _VSTD::move(*__first);
return __result;

有人可以给我建议如何解决这个问题吗?

Can somebody give me advice how to solve this?

推荐答案


有人可以给我建议如何解决吗?

Can somebody give me advice how to solve this?

当删除 std :: vector 中的非最后一个元素时,它必须将所有元素移到其后。可以通过移动分配(对于C ++ 11或更高版本)或元素的复制分配运算符来完成。因此,要解决此问题,您需要为类 Brick 提供此类运算符,使用不必移动 std :: list <之类的元素的容器/ code>或 std :: set 等或存储智能指针而不是对象本身。

When you delete a non last element in a std::vector, it has to move all elements behind it. It can be done either by move-assignment (for C++11 or later) or copy assignment operator for an element. So to solve it, you either need to provide such operator for class Brick, use container that does not have to move elements like std::list or std::set etc or store smart pointers instead of objects themselv.

这篇关于无法分配对象,因为其副本分配运算符被隐式删除错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆