c++ - How to call a method inside the operator> method if the methord called needs to change data members? -


i built own minheap requires me overload operators of classes want push it. have region class method called findsmallestcity. method loops through road objects (each have 2 cities) , returns smallest city in road inside region.

my comparison operators need know of 2 regions has smaller indexed city (cities integer values) because if 2 regions have same number of roads determines "smaller" 1 has lower indexed city in it.

here code operators , 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; } 

is there way around errors getting say:

error: passing ‘const region’ ‘this’ argument of ‘int region::findsmallestcity()’ discards qualifiers [-fpermissive]| 

just make method const:

int region::findsmallestcity() const { ... } 

this lets compiler know don't intend change region, safe use const region objects.


Comments

Popular posts from this blog

javascript - DIV "hiding" when changing dropdown value -

Does Firefox offer AppleScript support to get URL of windows? -

android - How to install packaged app on Firefox for mobile? -