为什么我在功能bool operator<(const Node& otherNode) //const
中没有放入const
时收到错误?C++过载运算符<错误
stl_algo.h:91: error: passing 'const Node' as 'this' argument of 'bool Node::operator<(const Node&)' discards qualifiers
所有的重载操作符应该保持不变吗?
class Node {
public:
double coordinate;
bool operator==(const Node& other) const{
return coordinate == other.coordinate;
}
bool operator<(const Node& other) const{
return coordinate < other.coordinate;
}
};
是我在编程测验,然后我慌了,因为比较超载运营商是不工作的话,我只是把常量,它工作,但我怀疑是否所有的操作符都必须是const。现在我明白了。谢谢。 – BRabbit27