title: vector iterator incompatible c++
body: <p>I've searched all over stackoverflow but just can't seem to find what i'm looking for, so here goes..
  I have 2 custom classes one being used in the second and when i'm trying to sort the second class.. which is essentially a vector of the first class it throws the error : -        message 0x010c3e18 "vector iterators incompatible"  const wchar_t *at vc\include\vector line 238 .. here is my code :</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;conio.h&gt;
  #include &lt;cmath&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  #include &lt;fstream&gt;
  #include &lt;algorithm&gt;
  
  using namespace std;
  
  class cityPhone {
  private: 
      string cityName;
      string cityCode;
  public:
   void setCode(string code){
      cityCode=code;
  }
   void setName(string name){
      cityName=name;
  }
   cityPhone(){
      cityName="Varna";
      cityCode="0888123123";
  }
   cityPhone(string name, string code){
      cityName=name;
      cityCode=code;
  }
   string getCity(){
  return cityName;    
  }
   string getCode(){
      return cityCode;
  }
  };
  
  //struct {               //ive used these 2 also and still it doesnt work
  //        bool operator()(cityPhone a, cityPhone b)
  //        {   
  //            if (a.getCity().compare(b.getCity())&gt;0)return true;
  //  return false;
  //        }   
  //    } cmpCity;
  // struct {
  //        bool operator()(cityPhone a, cityPhone b)
  //        {   
  //           if (a.getCode().compare(b.getCode())&gt;0)return true;
  //  return false;
  //        }   
  //    } cmpCode;
  
  bool cmpCity(cityPhone a, cityPhone b) // i'm using these 2 in the sorting method
          {   
              if (a.getCity().compare(b.getCity())&gt;0)return true;
      return false;
          }   
  
  
  bool cmpCode(cityPhone a, cityPhone b)
          {   
             if (a.getCode().compare(b.getCode())&gt;0)return true;
      return false;
  }
  
   class phoneDirectory {
   private :
       vector&lt;cityPhone&gt; data;
   public:
       phoneDirectory (string path){
          read(path); 
      }
       phoneDirectory (){
          data=vector&lt;cityPhone&gt;();   
      }
  
     void read(string path){
          cout&lt;&lt;endl;
          try {
  string line;
    ifstream myfile (path);
    cityPhone bla = cityPhone();
    if (myfile.is_open())
    {
      while ( getline (myfile,line) )
      {
          try{
        bla = cityPhone(line.substr(0,line.find_first_of(" ")),line.substr(line.find_first_of(" ")+1));
        data.push_back(bla);
          }
          catch(exception){ }
      }
      myfile.close();
    }
  
    else cout &lt;&lt; "Unable to open file"; 
          } catch (exception) {}
  }
  
     void addCityPhone(string city,string phone){
         try{
     data.push_back(cityPhone(city,phone)); 
         }
         catch(exception){
         cout&lt;&lt;"Error adding item "&lt;&lt;endl;
         }
     }
  
     void delCityPhone(int index){
         try{
     vector&lt;cityPhone&gt;::iterator p=data.begin();
     p+=index;
     data.erase(p);
         } 
         catch(exception){
         cout&lt;&lt;"Error deleting item with index "+index&lt;&lt;endl;
         }
     }
  
        cityPhone getCityPhone(unsigned index){
         try{
     vector&lt;cityPhone&gt;::iterator p=data.begin();
     p+=index;
     return *p;
         }
         catch(exception){
         cout&lt;&lt;"Error deleting item with index "+index&lt;&lt;endl;
         return cityPhone();
         }
     }
  
        vector&lt;cityPhone&gt; getData(){
        return data;
        }
  
     void phoneChange(string city, string newPhone){
     try{
         int i=0;
         vector&lt;cityPhone&gt;::iterator p=data.begin();
         for(p=data.begin();p&lt;data.end();p++,i++){
         if (getCityPhone(i).getCity().compare(city)==0){
             string oldPhone = getCityPhone(i).getCode();
             getCityPhone(i).setCode(newPhone);
             cout&lt;&lt;"Phone of city "+city + " was changed from "+oldPhone + " to " + newPhone&lt;&lt;endl;
             return;
         }
         cout&lt;&lt;"No such city exists!\n";
         }
     }
     catch(exception){
     cout&lt;&lt;"Error changing phone"&lt;&lt;endl;
          }
     }
  
      friend istream&amp; operator&gt;&gt;(ostream&amp; out,phoneDirectory a);
      friend ostream&amp; operator&lt;&lt;(ostream&amp; out,phoneDirectory a);
   };
  
  istream&amp; operator&gt;&gt;(istream&amp; in,phoneDirectory&amp; a){
      string city,phone;
  in &gt;&gt; city &gt;&gt; phone;
  a.addCityPhone(city,phone);
  return in;
  }
  
  ostream&amp; operator&lt;&lt;(ostream &amp;out, cityPhone a){
  return out &lt;&lt; a.getCity()  &lt;&lt;" " &lt;&lt; a.getCode() &lt;&lt;endl;
  }
  
         void sortByCity(phoneDirectory a){//with these two i try to sort
             vector&lt;cityPhone&gt;::iterator p=a.getData().begin();
             vector&lt;cityPhone&gt;::iterator q=a.getData().end();
          std::sort(p,a.getData().end(),cmpCity);
         }
  
         void sortByCode(phoneDirectory a){
             vector&lt;cityPhone&gt;::iterator p=a.getData().begin();
             vector&lt;cityPhone&gt;::iterator q=a.getData().end();
          std::sort(p,q,cmpCode);//i've tried with std::sort(a.getData().begin(),a.getData().end(),cmpCode) still no effect
         }
  
   //bool wayToSortCity (cityPhone a, cityPhone b){        //i've tryed also with these , still no effect
   //if (a.getCity().compare(b.getCity())&gt;0)return true;
      //return false;
   //}
  
   // bool wayToSortCode (cityPhone a, cityPhone b){
   //if (a.getCode().compare(b.getCode())&gt;0)return true;
      //return false;
   //}
  
  int main()
  {
  
      phoneDirectory test("C:\\t.txt");
      sortByCity(test);//here it crashes
  
  for(unsigned i=0;i&lt;test.getData().size();i++)
        cout&lt;&lt;test.getCityPhone(i);
  
      cin&gt;&gt;test;
      //test.sortBy(1);
  
      system("pause");
      return 0;
  }
  </code></pre>