2010-09-19 59 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