[英]Error while removing from deque
I have an std::deque<CustomType>
, I am trying to remove a member whose location I do not know. 我有一个
std::deque<CustomType>
,我试图删除一个我不知道其位置的成员。 Therefore, I am first finding it and then removing it. 因此,我首先找到它,然后将其删除。
/*
Remove from - members, which is the private variable of std::deque<User> type
*/
void Group::remove_member(User u) {
if(this->is_member(u)) {
std::deque<User>::iterator iter;
iter = std::find(this->members.begin(), this->members.end(), u);
if(iter != this->members.end()) {
this->members.erase(iter);
}
}
}
However, the compiler (GCC) is throwing an error which seems like missing Operator Overloading. 但是,编译器(GCC)抛出了一个错误,似乎是缺少运算符重载。
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/algorithm:62,
from Group.cpp:4:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_algo.h: In function ‘_RandomAccessIterator std::__find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = std::_Deque_iterator<User, User&, User*>, _Tp = User]’:
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_algo.h:4224: instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = std::_Deque_iterator<User, User&, User*>, _Tp = User]’
Group.cpp:36: instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_algo.h:174: error: no match for ‘operator==’ in ‘__first.std::_Deque_iterator<_Tp, _Ref, _Ptr>::operator* [with _Tp = User, _Ref = User&, _Ptr = User*]() == __val’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_algo.h:178: error: no match for ‘operator==’ in ‘__first.std::_Deque_iterator<_Tp, _Ref, _Ptr>::operator* [with _Tp = User, _Ref = User&, _Ptr = User*]() == __val’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_algo.h:182: error: no match for ‘operator==’ in ‘__first.std::_Deque_iterator<_Tp, _Ref, _Ptr>::operator* [with _Tp = User, _Ref = User&, _Ptr = User*]() == __val’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_algo.h:186: error: no match for ‘operator==’ in ‘__first.std::_Deque_iterator<_Tp, _Ref, _Ptr>::operator* [with _Tp = User, _Ref = User&, _Ptr = User*]() == __val’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_algo.h:194: error: no match for ‘operator==’ in ‘__first.std::_Deque_iterator<_Tp, _Ref, _Ptr>::operator* [with _Tp = User, _Ref = User&, _Ptr = User*]() == __val’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_algo.h:198: error: no match for ‘operator==’ in ‘__first.std::_Deque_iterator<_Tp, _Ref, _Ptr>::operator* [with _Tp = User, _Ref = User&, _Ptr = User*]() == __val’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_algo.h:202: error: no match for ‘operator==’ in ‘__first.std::_Deque_iterator<_Tp, _Ref, _Ptr>::operator* [with _Tp = User, _Ref = User&, _Ptr = User*]() == __val’
See my comment . 看我的评论 。
I have a feeling you have not declared
bool User::operator==(const User&)
我觉得您尚未声明
bool User::operator==(const User&)
The error is telling you that its attempting to do *iterator == ...
inside the implementation of std::find
. 该错误告诉您在
std::find
的实现中尝试执行*iterator == ...
The problem, however, is that you have not overloaded operator==
for User
. 但是,问题在于您没有为
User
重载operator==
。 Try declaring a member function inside User
as follows... 尝试按以下说明在
User
内部声明成员函数...
bool operator==(const User&);
Now, define it to provide some meaningful semantic equality between User
s, otherwise std::find
doesn't know how to compare them. 现在,定义它以在
User
之间提供有意义的语义相等,否则std::find
不知道如何比较它们。
As a side note, why doesn't Group::remove_member
take const User&
rather than User
? 附带说明一下,为什么
Group::remove_member
采用const User&
而不是User
?
Like @oldrinb mentioned, you need to tell explicitly how to test the equality of two User
class. 就像提到的@oldrinb一样,您需要明确告诉如何测试两个
User
类的相等性。 Otherwise the find
algorithm won't be able to locate the item you look for. 否则,
find
算法将无法找到您要查找的项目。 This is done by overriding the ==
operator. 这是通过覆盖
==
运算符完成的。
#include <iostream>
#include <deque>
#include <algorithm>
using namespace std;
class MyCustomClass{
public:
MyCustomClass(int id) : id_(id) { }
// Would produce the same error without this
bool operator==(const MyCustomClass& b){
return id_ == b.id_;
}
int id(){ return id_; }
private:
int id_;
};
int main(void){
deque<MyCustomClass> q;
q.push_back(MyCustomClass(1));
q.push_back(MyCustomClass(2));
q.push_back(MyCustomClass(3));
deque<MyCustomClass>::iterator it =
find(q.begin(), q.end(), MyCustomClass(2));
if ( it != q.end() ){
printf("Found\n");
q.erase(it);
}else{
printf("Not Found!\n");
}
for(it = q.begin(); it != q.end() ; it++)
printf("%d ", it->id());
printf("\n");
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.