2010-09-19 57 views
2

我試圖重寫操作<如下所示:如何重寫操作<

內部節點:

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<Node<T>*> m_heapVector; 

我檢查了通話,它沒有調用覆蓋的操作符。

回答

4

這是因爲你比較指針,

你必須使它:

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

和調整操作相應

bool operator<(const Node &other) const; 
+0

打我給它。 「相應地調整運算符」的意思是'節點 ::運算符<()'應該把一個引用而不是一個指針指向'other'。 – 2010-09-19 16:29:37

+0

@Ben沒問題,固定運營商 – Anycorn 2010-09-19 16:32:33

+0

@Roy,通知運營商應該是'(const Node&other)' – Anycorn 2010-09-19 16:55:35