我正在为我的学校做一个项目(我还是初学者),我遇到了以下问题:
"[Error] no match for 'operator==' (operand types are 'Vehicle' and 'const Vehicle')"
Run Code Online (Sandbox Code Playgroud)
Vehicle 在我的项目中成为一个班级.
这就是给我错误的原因:
int DayLog::findWaitingPosistion(Vehicle const& v){
if (find(waitingList.begin(),waitingList.end(),v) != waitingList.end())
return 1;
}
Run Code Online (Sandbox Code Playgroud)
waitingList是一个Vehicle对象的向量.
我搜索过,找不到答案,即使我有很多类似的问题我尝试了一切,但没有任何效果.提前致谢.
使用 find 的最低要求是指定operator==函数。如果找到了您的类型,这就是std::find它在遍历向量时使用的内容。
这样的事情是必要的:
class Vehicle {
public:
int number;
// We need the operator== to compare 2 Vehicle types.
bool operator==(const Vehicle &rhs) const {
return rhs.number == number;
}
};
Run Code Online (Sandbox Code Playgroud)
这将允许您使用查找。请参阅此处的实例。