问题: [英] Problem with operator <
本文介绍了问题:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个问题,我在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())& .GetData()));
}
在Heap.h:
模板< 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;
}
}
在Vehicle.h:
bool operator < (const Vehicle& otherVehicle)const;
在Vehicle.cpp中:
bool Vehicle :: operator<(const Vehicle& otherVehicle)const {
return(GetDistance()> otherVehicle.GetDistance());
}
在main.cpp中:
。
..
节点< Vehicle *> a(car1);
Node< Vehicle *> b(car2);
Heaps< Vehicle *>堆;
Node< Vehicle *> * p =& a;
Node< Vehicle *> * q =& b;
heap.Insert(p);
heap.Insert(q);
heap.ExtractMin() - > GetData() - > Show();
。
..
为什么不能做这项运动?其中,
因为你使用的是Vehicle *,而不是Vehicle。
I have a problem with the operator < that i wrote:
in 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()));
}
in 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;
}
}
in Vehicle.h:
bool operator< (const Vehicle& otherVehicle) const;
in Vehicle.cpp:
bool Vehicle::operator<(const Vehicle& otherVehicle) const {
return (GetDistance() > otherVehicle.GetDistance());
}
in 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();
.
..
Why it doen't do the compeare ? with opeartor < , note: it pass the compiler.
解决方案
Because you used Vehicle*, not Vehicle.
这篇关于问题:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文