getter 使用 getline 时函数不工作
getter function is not working when getline is used
我正在使用 getline 从用户那里获取字符串输入,如下所示。
cout << "Enter full name : ";
cin >> fullname;
cin.ignore();
getline(cin, fullname);
getline 工作正常我可以用白色写全名space。但问题是当使用 getter 函数时我无法获得全名。
if (strcmp(u[i]->getemail(), email) == 0) { // if entered email matches the email
cout << "User name -->" << u[i]->getusername()<<endl; //member details is displayed
cout << "Full name -->" << u[i]->getfullname()<<endl;
cout << "Password -->" << u[i]->getpassword()<<endl;
flag = true;
break;
下面是我的 setter/getter 变量 fullname
的函数
string User::getfullname()
{
return fullname;
}
void User::setfullname(string fullname)
{
fullname = fullname;
}
这是构造函数
User::User(char* username, char* password, string infullname, char* email)
{ //constructor
strcpy(this->username, username);
strcpy(this->password, password);
setfullName(infullname);
strcpy(this->email, email);
}
这是输出
https://www.dropbox.com/s/oya9wtswd3phq7o/error.jpg?dl=0
我是C++的新手,如果您能指出错误,我将不胜感激。
通过阅读您的代码,我认为问题可能来自您的 setter function
。
void User::setfullname(string fullname)
{
fullname = fullname;
}
函数参数和您的 class 属性同名。您需要使用 this 关键字来识别。或者更改函数的参数名称。
类似这样的东西:
void User::setFullName(string str)
{
this->fullName = str;
}
我正在使用 getline 从用户那里获取字符串输入,如下所示。
cout << "Enter full name : ";
cin >> fullname;
cin.ignore();
getline(cin, fullname);
getline 工作正常我可以用白色写全名space。但问题是当使用 getter 函数时我无法获得全名。
if (strcmp(u[i]->getemail(), email) == 0) { // if entered email matches the email
cout << "User name -->" << u[i]->getusername()<<endl; //member details is displayed
cout << "Full name -->" << u[i]->getfullname()<<endl;
cout << "Password -->" << u[i]->getpassword()<<endl;
flag = true;
break;
下面是我的 setter/getter 变量 fullname
的函数 string User::getfullname()
{
return fullname;
}
void User::setfullname(string fullname)
{
fullname = fullname;
}
这是构造函数
User::User(char* username, char* password, string infullname, char* email)
{ //constructor
strcpy(this->username, username);
strcpy(this->password, password);
setfullName(infullname);
strcpy(this->email, email);
}
这是输出 https://www.dropbox.com/s/oya9wtswd3phq7o/error.jpg?dl=0
我是C++的新手,如果您能指出错误,我将不胜感激。
通过阅读您的代码,我认为问题可能来自您的 setter function
。
void User::setfullname(string fullname)
{
fullname = fullname;
}
函数参数和您的 class 属性同名。您需要使用 this 关键字来识别。或者更改函数的参数名称。
类似这样的东西:
void User::setFullName(string str)
{
this->fullName = str;
}