c++ - 使用 getline 时 getter 函数不起作用

标签 c++ getline

我正在使用 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/

相关文章:

c++ - std::atomic_bool 标志的内存顺序

haskell - 将用户输入(整数)转换为条件打印语句

c++ - ifstream gcount 在 getline 字符串过载时返回 0

c - 为什么 getline 在 C 函数中失败?

c++ - 使用 getline C++ 获取对象的名称

c++ - 使程序与输入和文件一起工作

c++ - 如何正确处理来自 MSFTEDIT_CLASS (RichEdit) 控件的 Windows 消息?

c++ - MSVC 10 + Luabind + std::vector == 拒绝编译

c++ - 如何获取 WinRT 上的逻辑 CPU 数量?

c++ - 怀疑在枚举中解析 namespace ?