的std ::删除原因编译错误
我想使用的答案为question并得到奇怪的错误 -的std ::删除原因编译错误
/usr/include/c++/4.6/bits/stl_algo.h:162: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* with _Iterator = User*, _Container = std::vector, __gnu_cxx::__normal_iterator<_Iterator, _Container>::reference = User& == __val’
我使用Linux(Ubuntu的64位),也许这是一个问题。 在此先感谢。
UPDATE: 代码,我使用remove()方法:
myVec.erase(std::remove(myVec.begin(), myVec.end(), vecMember), myVec.end());
的std ::删除通话operator==
,你需要重载它为您User type
:
假设你的名字比较用户:
bool operator==(const User& lhs, const User& rhs)
{
return lhs.name == rhs.name;
}
如果您仔细阅读,编译器消息会告诉您究竟丢失了什么。
或者使用std::remove_if与拉姆达如果你使用C++ 11
myVec.erase(std::remove(myVec.begin(), myVec.end(),
[](const User& u){ return u.name == "name"; }), vec.end());
或者使用'remove_if'。 – 2013-02-17 11:08:50
如果你想使用lambdas(OP使用g ++ 4.6),不要忘记使用'-std = C++ 0x'。 – Zeta 2013-02-17 11:11:40
非常感谢!对不起,坏的问题,我不是一个好的C++程序员... – avrilfanomar 2013-02-17 11:12:39
你可以张贴再现误差小的代码示例? – juanchopanza 2013-02-17 11:06:38