我构建了自己的 minHeap,这要求我重载所有要推送给它的类的运算符。我有一个带有名为 findSmallestCity 的方法的 Region 类。此方法循环遍历道路对象(每个对象有两个城市),然后返回该区域内任何道路中最小的城市。
我的比较操作员需要知道两个地区中哪个地区的索引城市较小(城市是整数值),因为如果两个地区的道路数量相同,它会确定哪个地区的索引城市“更小”。
以下是运算符和 findSmallestCity 的代码:
int Region::findSmallestCity(){
curRoad = head;
int smallestCity = curRoad->getCityA();
while(curRoad != 0){
if(curRoad->getCityA() <= smallestCity) smallestCity = curRoad->getCityA();
if(curRoad->getCityB() <= smallestCity) smallestCity = curRoad->getCityB();
curRoad = curRoad->nextRoad;
}
return smallestCity;
}
bool operator<( const Region &lhs, const Region &rhs)
{
if(lhs.numRoads < rhs.numRoads) return 1;
else if(lhs.findSmallestCity() < rhs.findSmallestCity()) return 1;
else return 0;
}
bool operator>( const Region &lhs, const Region &rhs)
{
if(lhs.numRoads > rhs.numRoads) return 1;
else if(lhs.findSmallestCity() > rhs.findSmallestCity()) return 1;
else return 0;
}
bool operator<=( const Region &lhs, const Region &rhs)
{
if(lhs.numRoads < rhs.numRoads) return 1;
else if(lhs.findSmallestCity() < rhs.findSmallestCity()) return 1;
else return 0;
}
bool operator>=( const Region &lhs, const Region &rhs)
{
if(lhs.numRoads > rhs.numRoads) return 1;
else if(lhs.findSmallestCity() > rhs.findSmallestCity()) return 1;
else return 0;
}
有没有办法解决我所说的错误:
error: passing ‘const Region’ as ‘this’ argument of ‘int Region::findSmallestCity()’ discards qualifiers [-fpermissive]|