Estefânia Moura
Estefânia Moura

Reputation: 11

How can I define Operator == when comparing vectors of classes?

I want to use the fonction find in order to find a element in a vector of struct (with the atributs x and y),

But i'm getting the error with no matching operator==. I tried to redefine it, but i'm still getting the same error message:

Error: no match for « operator== » (operand types are « const coordenates » and « const coordenates ») { return *__it == _M_value; }

struct coordenates {
int x;
int y;

bool operator ==(const coordenates &a){
    if (x == a.x && y == a.y)
        {return true;}
    return false;
}

 bool findInVector(const std::vector<coordenates> &vecOfElements, const int &i, const int &j)
{
    bool presence;
    coordenates element;
    element.x = i;
    element.y = j;

if(std::find(vecOfElements.begin(), vecOfElements.end(), element) == vecOfElements.end()) {
    presence = false;}
else{presence = true;}
    return presence;
}

Upvotes: 1

Views: 74

Answers (1)

Hatted Rooster
Hatted Rooster

Reputation: 36483

It's passed as a const coordinates so your operator== must be marked const as well to match:

bool operator ==(const coordenates &a) const

Upvotes: 4

Related Questions