简体   繁体   中英

invalid operands to binary expression when using std::replace c++

I have a vector that contains usernames and passwords. I am trying to do a "reset new password" function whereby I first search for the username in the vector whether the username existed, and then next the user will be prompt to key in the new password and the old password will be replaced by the new one.

but I am encountering this error

Invalid operands to binary expression ('User' and 'const std::__1::basic_string<char>')

Because of this line.

replace(userDetails.begin(), userDetails.end(), oldPassword, newPassword);

part of my codes related to the problem

string name;
cout << "Please enter a user name that the password will be reset \n";
cin >> name;

for (int i =0; i<userDetails.size(); i++) {
    if (userDetails[i].getUserName() == name) {
        string newPassword;
        string oldPassword;
        oldPassword = userDetails[i].getPassword();
        cout << "Please enter a new password" << endl;
        cin >> newPassword;
        replace(userDetails.begin(), userDetails.end(), oldPassword, newPassword);
        cout << userDetails[i].getPassword();
    }
}

I am not sure what I should do to achieve my desired results. Please help. Thanks

The first problem is in the line

replace(userDetails.begin(), userDetails.end(), oldPassword, newPassword);

you are trying to replace User object with std::string object.

The second problem (as mentioned by nm ) — you don't need to use range-based functions ( replace() or other) inside the loop when you have your hands on the proper User object already.

You can use std::find_if() function to find the User object with proper name instead of writing the loop. In C++11 it's possible like this:

auto user = find_if(userDetails.begin(), userDetails.end(),
                   [&name](const User& u){return u.getUserName()==name;});

In pre C++11 code you'll need a separate predicate function instead of lambda / your own search loop (just break; when the user is found).

If the user is found you can set the password:

if(user != userDetails.end()){
    // (ask for the new password here)
    user->setPassword(newPassword); // assuming you have a setter for password field
}

It seems like your userDetails container holds User objects, yet you are treating it as if it contained std::string s. You should be replacing the password of userDetails[i] . How to do that depends on the details of the User class.

You probably want something like this, using std::find_if with a suitable predicate to find the first user with username equal to name :

// find the right User
auto it = std::find_if(userDetails.begin(),
                       userDetails.end(),
                       [&name](const User& user) 
                       { return user.getUserName() == name;})

then replace the password for that user:

if (it != userDetails.end()) {
  // we found a user with username == name
  it->setPasswd(newPasswd);
} else
  // no user found with that username
  std::cout << "User " << name << " not found" << std::endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM