Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace algorithm c++ invalid operands to binary expression

I am so confused about the error:

error:invalid operands to binary expression ('Record' and 'const Record')

I am unable to understand why my code:

replace(phoneBook.begin(),phoneBook.end(),old_r,new_r) 

will get the error. What means by const Record?

using namespace std;
class Record{
     public:
          string name;
          int number;
};
int main(){


     vector <Record> phoneBook;
     string command;
     while ( cin >> command) {

          if( command == "Update"){ // Handle the Update command
                Record new_r;
                Record old_r;
                int number;
                cin>>new_r.name>>new_r.number;
                vector<Record>::iterator itr;
               for(itr=phoneBook.begin();itr!=phoneBook.end();itr++){
                    if((*itr).name==new_r.name){
                        old_r.number=(*itr).number;
                        old_r.name=(*itr).name;
                    }
               }

                replace(phoneBook.begin(),phoneBook.end(),old_r, new_r);


          }

     }

}
like image 324
Edison Lo Avatar asked May 12 '26 11:05

Edison Lo


1 Answers

Give Record a operator == and it'll compile. Something like:

class Record{
     public:
      string name;
      int number;
      bool operator==(const Record& rhs){
          if ((this->name==rhs.name) and (this->number==rhs.number))
              return true;
          return false;
      }
};
like image 165
Pieter Bruegel the Elder Avatar answered May 14 '26 02:05

Pieter Bruegel the Elder