Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ [Error] no match for 'operator==' (operand types are 'Vehicle' and 'const Vehicle')

Tags:

c++

I'm working on a project for my school (I'm still a beginner) and I have come across the following problem:

"[Error] no match for 'operator==' (operand types are 'Vehicle' and 'const Vehicle')" 

Vehicle being a class in my project.

This is what is giving me the error:

int DayLog::findWaitingPosistion(Vehicle const& v){
    if (find(waitingList.begin(),waitingList.end(),v) != waitingList.end())
        return 1;
}

waitingList is a vector of Vehicle objects.

I searched and couldn't find an answer even though there were many similar questions to mine I tried everything but nothing worked. Thanks in advance.

like image 756
SinLortuen Avatar asked Jan 27 '16 23:01

SinLortuen


1 Answers

The minimum requirements for using find is a specified operator== function. This is what std::find uses as it iterates through the vector if it has found your type.

Something like this will be necessary:

class Vehicle {
    public:
    int number;
    // We need the operator== to compare 2 Vehicle types.
    bool operator==(const Vehicle &rhs) const {
        return rhs.number == number;
    }
};

This will allow you to use find. See a live example here.

like image 133
Fantastic Mr Fox Avatar answered Oct 19 '22 23:10

Fantastic Mr Fox