gpt4 book ai didi

c++ - 无法将数据从txt文件写入对象

转载 作者:行者123 更新时间:2023-11-28 00:39:23 24 4
gpt4 key购买 nike

我发现这里的编码有问题。我需要从一个文本文件中读取,然后写入一个对象。但是,我可能做不到。对象中的值似乎未初始化。

void readPolynomial(string filename, polynomial& p)
{
//Read in the terms of the polynomial from the data file.
//Terms in the data file are arranged in descending order by the exponent.
//One term per line (coefficient followed by exponent), and there is no blank line.
term temp = term();
double c = 0;
int e = 0;
ifstream fin;
fin.open(filename);

while(!fin.eof())
{
fin >> c >> e;
temp = term(c, e);
p.addTerm(temp);
}
fin.close();
}

这是类术语的头文件。

默认构造函数:

term()
{
coef = 0;
exp = 0;
}

term::term(double c, int e)
{
c = coef;
e = exp;
}

最佳答案

看起来您在双参数构造函数中交换了参数和成员变量。尝试:

term::term(double c, int e)
{
coef = c;
exp = e;
}

关于c++ - 无法将数据从txt文件写入对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19593338/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com