简体   繁体   中英

C++ Classes and accessing

I am getting an error, and being new to C++ I have absolutely no idea what it means, and I also know that you should not get passwords this way, but it is the only way I could think of to test myself in more difficult areas of C++, I have just started this and already get stumped by this.

class user
{
private:
    int security;
    string password;
public:
    string username, email;
    int age;
    string signup()
    {
        int num;
        cout << "Welcome new user!\nPlease enter your username:\n";
        cin >> username;
        cout << "What is your email?\n";
        cin >> email;
        cout << "What is your age?\n";
        cin >> age;
        cout << "Make a password:\n";
        cin >> password;
        cout << "Enter a number:\n";
        cin >> num;
        security = mnet::random(num);
        cout << "Your security code is " << security << endl;
        return username;


    }
};
int main()
{
    string LorS;
    user cprofile;
    cout << "Welcome to MatrixNet! Login or Sign up?[L/S]\n";
    cin >> LorS;
    if(LorS == "S" || LorS == "s") {
        cprofile = cprofile.signup();
    }



    return 0;
}

The errors I am getting:

In function 'int main()':|
|55|error: no match for 'operator=' (operand types are 'user' and 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}')

|20|note: candidate: user& user::operator=(const user&)

|20|note:   no known conversion for argument 1 from 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'const user&'

|20|note: candidate: user& user::operator=(user&&)

|20|note:   no known conversion for argument 1 from 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' to 'user&&'|

Line 55:

    cprofile = cprofile.signup();

signup() returns a std::string , but you're trying to assign it to a user , which as the compiler is telling you (rather cryptically, mind you), you cannot do:

 note: no known conversion for argument 1 from 'std::__cxx11::string {aka std::__cxx11::basic_string}' to 'const user&'` 

I'd suggest dropping the assignment on line 55 and simply calling cprofile.signup() . In object-oriented programming, objects are stateful, which means they contain state, such as your security , password , etc. Your signup() function sets this state on the object it's called on, so by simply saying cprofile.signup() , cprofile modifies itself appropriately. This is also the basis for class encapsulation .

You cannot assign a string to a class object as you attempted in writing this statement:

cprofile = cprofile.signup();

if you simply wanted to store the string returned by your signup function then just declare a new string variable and use it:

string LorS;
string userName;
user cprofile;
cout << "Welcome to MatrixNet! Login or Sign up?[L/S]\n";
cin >> LorS;
if(LorS == "S" || LorS == "s") {
    userName = cprofile.signup();
}

Signup simply returns the username as a string, as well as setting up your account. Thus, trying to assign cprofile to the return value of signup () makes no sense without a constructor that receives a std::string. It looks like you want the side effects of signup not the return value, so just run cprofile.signup(). If you didn't understand this, you probably need to learn more.

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