我的 setter 可以获取参数,但实际上不能?

没有人0011

所以我正在制作某种存储人们信息的注册表。我的问题是我有一个类,它有一个人的多个 setter 和 getter(名字、出生日期、出生地等)。当我尝试从文本文件中读取信息时,我无法向设置器提供我从文件中获得的参数。

我制作了一个临时文件,我试图找出问题所在,但我真的无处可去。循环无关紧要,如果我想做的事情不在循环中并且文件中只有一行,则问题保持不变

int main(){
    std::string firstName;
    std::string lastName;
    std::string phoneNumber;
    std::string birthPlace;
    std::string birthDate;
    std::string Profession;

     Contacts newContact = Contacts();


    std::ifstream savedContacts("ContactList.txt");

    do{
        std::getline(savedContacts, firstName, ';');
        std::getline(savedContacts, lastName, ';');
        std::getline(savedContacts, phoneNumber, ';');
        std::getline(savedContacts, birthPlace, ';');
        std::getline(savedContacts, birthDate, ';');
        std::getline(savedContacts, Profession, ';');
 /* 
 in this case, this setter doesn't work,
 it doesn't get the string stored in firstName,after this,the program 
 crashes
 */
        newContact.setFirstname(firstName);
        std::cout<<newContact.getFirstname();

/*
and just to make sure that the reading of the file was successful
if i print out one of these strings, like this, it works perfectly 
*/
 std::cout<<firstName;
    }while(std::getline(savedContacts, firstName));

有趣的是,如果我这样做: newContact.setFirstname("Karen"); 然后 setter 工作得很好,还有我的 getter

这就是我的联系人类中的 setter 和 getter 的样子

    std::string Contacts::setFirstname(std::string firstName) {
    this->firstName = firstName;
}

std::string Contacts::getFirstname() {
    return firstName;
}

这是在 txt 文件中:

John;Wick;1800181819;Paris;11.09.1990;Plumber;
Anthony;Joshua;192918180;Manchester;10.08.1994;Teacher;
米尔科
// Several issues here:
std::string Contacts::setFirstname(std::string firstName) {
    this->firstName = firstName;
}

// Don't return a copy. Setters will usually only *set*
// If you WANT to return the value, you should NOT return a copy
// but a reference or a const reference
// You are copying AGAIN for the argument; take a const reference
// And probably avoid a copy
// Change parameter name and you could spare the this->
// and return a value; you declared a return type but no return statement
// Which means the code you posted doesn't compile
const std::string& Contacts::setFirstname(const std::string& newFirstName) 
{
    firstName = newFirstName;
    return firstName;
}

// Same here, return const reference and let the caller copy only if needed
// And make the getter const so it can work with const Contact
const std::string& Contacts::getFirstname() const {
    return firstName;
}

此外,关于您的问题,很可能是在调用 setFirstName 的代码中。你没有提供。请提供最小、完整和可验证的示例

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何防止我的 setter 方法中的参数被更改?

我可以在getter / setter中执行其他逻辑吗?

我可以在setter方法中编写验证逻辑吗?

春季+龙目岛:我可以@Autowired @Setter

我可以使用setter方法替换最终变量吗?

我可以在构造函数中使用setter吗?

我如何访问 javascript 对象上的 setter 和 getter?

由于表单验证,我不能在实体的 setter 中定义参数类型吗?

我不能在我的javascript类中使用setter,因为它不是函数

为什么我可以将 setter 与内部带有 builder 类的类链接在一起,但不能在普通类的实例上链接?

成员函数的'this'参数类型为'const',但是我的函数实际上不是'const'

我如何知道具有类型接口的参数是否实际上是结构?

我实际上可以从lambda返回以从方法返回并中断其执行吗?

我实际上可以使用ANSI转义序列保存几个光标位置吗?

我可以告诉C#可为空的引用方法实际上是对字段的空检查吗

为什么我不能将属性名称而不是“值”关键字放在setter中?

为什么我不能删除 Object.defineProperties 中定义的 getter/setter?

获取assert_select以向我展示元素实际上是什么

我可以在没有反射的情况下将setter(而不是值)作为参数传递给函数吗?

我应该总是继续使用`sink`构造函数或setter参数吗?

我可以使用具有公共属性的Realm + Parceler(不带getter / setter)吗?

我可以在没有相应 getter 的情况下对 Java setter 使用属性语法吗?

为什么我无法在 Angular 中订阅来自不同模块的 observable(而 setter 也可以)

我可以在Java MVC模型中使用getter和setter吗

我可以为“valuetype”的 getter 和 setter 设置不同的返回类型吗?特性?

使用powershell,我正在导入C#DLL。我似乎只能调用实例的getter方法,而不能调用setter方法。

我如何获取我实际上在node.js http请求中发送的http请求标头

谁能告诉我如何将getter和setter放在下面的代码上,以及getter和setter的作用是什么

我如何使用PojoCodecProvider忽略getter / setter