我正在尝试重写运算符<,如下所示:
节点内部:
bool operator <(const Node* other) {
return *(this->GetData()) < *(other->GetData());
}
车内:
bool operator <(const Vehicle &other) {
return this->GetKilometersLeft() < other.GetKilometersLeft();
}
调用运算符:
while (index > 0 && m_heapVector[index] < m_heapVector[parent(index)])
vector 定义:
vector<Node<T>*> m_heapVector;
我检查了该调用,它没有调用被覆盖的运算符。
最佳答案
这是因为您正在比较指针,
你必须做到:
*m_heapVector[index] < *m_heapVector[parent(index)]
并相应地调整运算符
bool operator<(const Node &other) const;
关于c++ - 如何覆盖运算符 <,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3746462/