C++ [错误] 不匹配“operator==”(操作数类型为“Vehicle”和“const Vehicle”)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35050065/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ [Error] no match for 'operator==' (operand types are 'Vehicle' and 'const Vehicle')
提问by SinLortuen
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.
Vehicle
成为我项目中的一个班级。
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.
waitingList
是Vehicle
对象的向量。
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.
我搜索并找不到答案,即使有很多与我类似的问题我尝试了所有方法但没有任何效果。提前致谢。
采纳答案by Fantastic Mr Fox
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.
使用 find 的最低要求是指定的operator==
函数。std::find
如果找到您的类型,这就是它在遍历向量时使用的内容。
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.
回答by Pete Baughman
The error message is pretty clear: The compiler is looking for an operator== function that compares two vehicles. The signature of such a method, were it to exist, would be something like
错误消息非常清楚:编译器正在寻找一个 operator== 函数来比较两辆车。这种方法的签名,如果存在的话,应该是这样的
bool operator==(const Vehicle& first, const Vehicle& second);
What's not as clear is whythis is happening. After all, you don't use an == operator anywhere in your code! Crummy compiler - complaining about something you didn't even do.
尚不清楚的是为什么会发生这种情况。毕竟,您不会在代码中的任何地方使用 == 运算符!糟糕的编译器 - 抱怨你甚至没有做过的事情。
To understand what's happening, you have to understand the 'find' method. This is a template method, and in C++ templates are pretty much super-fancy text find-and-replace (warning: massive simplification!). The code for 'find' is going to be generated on-the-fly for the types that you're using right before the compiler runs.
要了解发生了什么,您必须了解“查找”方法。这是一个模板方法,在 C++ 模板中几乎是超级花哨的文本查找和替换(警告:大规模简化!)。'find' 的代码将在编译器运行之前为您正在使用的类型即时生成。
You can check out how find is implemented here. In the unlikely event that cplusplus.com ever goes offline, I've included the relevant portion below*:
您可以在此处查看 find 是如何实现的。万一 cplusplus.com 脱机,我在下面列出了相关部分*:
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last) {
if (*first==val) return first; //<--- Notice the == operator
++first;
}
return last;
}
That's where the == is coming from! The compiler is going to automatically generate the find code for the type you've specified (vehicle). Then when it goes to compile, that generated code tries to use an operator== but there isn't one for vehicle. You're going to have to provide one with your vehicle class.
这就是 == 的来源!编译器将自动为您指定的类型(车辆)生成查找代码。然后在编译时,生成的代码会尝试使用运算符==,但没有用于车辆的运算符。您将不得不为您的车辆类别提供一个。
*Seriously though - check out that website. It shows you how all of this stuff works.
*说真的 -看看那个网站。它向您展示了所有这些东西是如何工作的。