我有与操作员<一个问题,即我写:问题的操作者<
在Node.h:
.
..
bool operator<(const Node<T>& other) const;
const T& GetData();
.
..
template <class T>
const T& Node<T>::GetData() {
return m_data;
}
template <class T>
bool Node<T>:: operator<(const Node<T>& other) const
{
return (*(this->GetData()) < *(other.GetData()));
}
在Heap.h
:
template<class T>
void Heap<T>::Insert(Node<T>* newNode) {
if (m_heap.size() == 0) {
m_heap.push_back(newNode);
}
else
DecreaseKey(newNode);
}
template<class T>
void Heap<T>::DecreaseKey(Node<T>* newNode) {
m_heap.push_back(newNode);
int index = m_heap.size();
while ((index > 1) && (m_heap[(index/2)-1] < (m_heap[index-1]))) { // doen't do the operator < !
Exchange(index,index/2);
index = index/2;
}
}
在车辆
。 h:
bool operator< (const Vehicle& otherVehicle) const;
在Vehi cle.cpp:
bool Vehicle::operator<(const Vehicle& otherVehicle) const {
return (GetDistance() > otherVehicle.GetDistance());
}
在main.cpp中
: 。
..
Node<Vehicle*> a(car1);
Node<Vehicle*> b(car2);
Heap<Vehicle*> heap;
Node<Vehicle*>* p = &a;
Node<Vehicle*>* q = &b;
heap.Insert(p);
heap.Insert(q);
heap.ExtractMin()->GetData()->Show();
.
..
为什么它不做竞争?与opeartor <,注意:它通过编译器。
请尽量使代码尽量少。如果您在上述所有代码中陈述问题,它会帮助阅读很多内容。 – 2010-09-21 11:34:49