我正在使用 getline 从用户那里获取字符串输入,如下所示。
cout << "Enter full name : ";
cin >> fullname;
cin.ignore();
getline(cin, fullname);
getline 工作正常我可以用空格写全名。但问题是在使用 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函数
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 函数
上。
void User::setfullname(string fullname)
{
fullname = fullname;
}
函数参数和类属性同名。您需要使用 this 来识别关键词。或者更改函数的参数名称。
类似这样的东西:
void User::setFullName(string str)
{
this->fullName = str;
}
关于c++ - 使用 getline 时 getter 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47689959/