views:

73

answers:

1

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.

+4  A: 

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;
aaa
Beat me to it. "adjust operator accordingly" means `Node<T>::operator<()` should take a reference, not a pointer, to `other`.
Ben Voigt
@Ben no problem, fixed operator
aaa
aaa
@aaa hey thanks. i did what you said, now my operator looks like this : bool operator <(const Node } and now i'm getting stack overflow.
Roy Gavrielov
@Roy on the other hand, I think your problem is you are having infinite recursion in your implementation of comparison operator
aaa
@aaa right, now no stack overflow but it's not entering the operator inside vehicle
Roy Gavrielov
Solved, by changing the operator in Node to be : bool operator <(const Node<T> } thanks all.
Roy Gavrielov