Roy Gavrielov
Roy Gavrielov

Reputation: 607

how to Override operator <

I'm trying to override operator < as the following :

inside Node :

bool operator <(const Node* other) {
  return *(this->GetData()) < *(other->GetData());
}

inside vehicle :

bool operator <(const Vehicle &other) {
  return this->GetKilometersLeft() < other.GetKilometersLeft();
}

invoking the operator :

while (index > 0 && m_heapVector[index] < m_heapVector[parent(index)])

vector definition :

vector<Node<T>*> m_heapVector;

I checked the call and it's not calling the overridden operators.

Upvotes: 2

Views: 339

Answers (1)

Anycorn
Anycorn

Reputation: 51475

this is because you are comparing pointers,

You have to make it:

*m_heapVector[index] < *m_heapVector[parent(index)]

and adjust operator accordingly

bool operator<(const Node &other) const;

Upvotes: 4

Related Questions