Domanda

I am trying to use answers for question and get strange error -

/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’

I'm using Linux(Ubuntu 64-bit) and maybe that is a problem. Thanks in advance.

UPDATE: code where I use remove():

myVec.erase(std::remove(myVec.begin(), myVec.end(), vecMember), myVec.end());
È stato utile?

Soluzione

std::remove calls operator==, you need to overload it for your User type:

assume you compare User by name:

bool operator==(const User& lhs, const User& rhs)
{
   return lhs.name == rhs.name;
}

If you read carefully, compiler message tells you what exactly is missing.

Alternatively use std::remove_if with lambda if you use C++11

myVec.erase(std::remove(myVec.begin(), myVec.end(), 
            [](const User& u){ return u.name == "name"; }), vec.end());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top