简体   繁体   English

C ++ vector :: erase抱怨重载解决方案并删除了运算符'=='

[英]C++ vector::erase complains about overload resolution & deleted operator '=='

I am on C++ using std::vector to store a list of class objects using vector::push_back . 我在C++使用std::vector使用vector::push_back存储类对象的列表。

My add function accepts the base class type so that the same method can be used across. 我的add函数接受基类类型,以便可以使用相同的方法。

Add(MyBaseClass object) {
  my_vector.push_back(object)
}

I have remove function to remove it by per item added 我有删除功能,可以按添加的每个项目删除它

Remove(MyBaseClass object) {
  my_vector.erase(std::remove(my_vector.begin(), my_vector.end(), object), my_vector.end());
}

The add works fine but my Remove method gives following error: 添加工作正常,但我的Remove方法给出以下错误:

overload resolution selected deleted operator '=='
            if (!(*__i == __value_))
              ~~~~ ^  ~~~~~~~~

I picked up the vector::erase code from here . 我从这里获取了vector :: erase代码。 What is wrong with my way of erasing an item? 我删除项目的方式有什么问题? Are there any other preferable ways to erase by item? 还有其他更可取的按项目擦除的方法吗?

The Multipass guarantee of the ForwardIterator requirement for remove to work on std::vector specifies that MyBaseClass must implement operator== . 要在std::vector上运行removeForwardIterator要求的 ForwardIterator 保证 ,指定MyBaseClass 必须实现operator==

Your class doesn't appear to implement this, and compilation therefore fails. 您的班级似乎没有实现此目标,因此编译失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM