Object cannot be assigned because its copy assignment operator is implicitly deleted error

杀马特。学长 韩版系。学妹 提交于 2020-01-04 05:41:06

问题


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?


回答1:


Can somebody give me advice how to solve this?

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.



来源:https://stackoverflow.com/questions/34752505/object-cannot-be-assigned-because-its-copy-assignment-operator-is-implicitly-del

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!