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);
}
}
}
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;
}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With