C++ 如何覆盖运算符<;
我试图覆盖运算符<,如下所示: 内部节点:C++ 如何覆盖运算符<;,c++,templates,operators,overriding,C++,Templates,Operators,Overriding,我试图覆盖运算符
bool operator <(const Node* other) {
return *(this->GetData()) < *(other->GetData());
}
bool操作符GetData())<*(其他->GetData());
}
车内:
bool operator <(const Vehicle &other) {
return this->GetKilometersLeft() < other.GetKilometersLeft();
}
bool操作符getkimmersleft()
调用运算符:
while (index > 0 && m_heapVector[index] < m_heapVector[parent(index)])
while(索引>0&&m_heapVector[索引]
矢量定义:
vector<Node<T>*> m_heapVector;
向量m_heapVector;
我检查了调用,但它没有调用重写的运算符。这是因为您正在比较指针 你必须做到:
*m_heapVector[index] < *m_heapVector[parent(index)]
*m_heapVector[索引]<*m_heapVector[父(索引)]
并相应地调整操作员
bool operator<(const Node &other) const;
bool操作符吃我的吧。“相应地调整操作员”表示节点:operator@Ben没问题,修好了operator@Roy,注意操作员应该是(const Node&other)
@aaa嘿,谢谢。我照你说的做了,现在我的操作符看起来是这样的:bool操作符@Roy另一方面,我认为你的问题是你在比较操作符的实现中有无限递归