gpt4 book ai didi

c++ - 编译以下代码时发生错误

转载 作者:行者123 更新时间:2023-12-02 10:57:26 25 4
gpt4 key购买 nike

当我尝试运行程序时,Visual Studio抛出如下错误:“错误:无法打开文件C:\ Users ... \ test1 \ Debug \ investment.obj。错误代码= 0x80070002。

我已经尝试了许多在线提到的方法,但是仍然无法正常工作。所以,我在想代码中是否有任何问题。

这是怎么回事?请帮忙。

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <numeric>

using namespace std;

struct Investment
{
string Name;
string BankAccount;
string SortCode;
float Investment;
float Contribution;
};

string Trim(string str)
{
str.erase(0, str.find_first_not_of(" \t\r\n"));
str.erase(str.find_last_not_of(" \t\r\n") + 1);
return str;
}

void Get_Data(const string& filename, vector<Investment>& data)
{
ifstream fin(filename);
if (!fin.is_open())
return;

string line;
if (!getline(fin, line))
return;

while (getline(fin, line))
{
istringstream sin(line);
vector<string> fields;
string field;

while (getline(sin, field, ','))
{
fields.push_back(field);
}

Investment inv;
inv.Name = Trim(fields[0]);
inv.BankAccount = Trim(fields[1]);
inv.SortCode = Trim(fields[2]);
inv.Investment = atof(Trim(fields[3]).c_str());
inv.Contribution = 0.0f;

data.push_back(inv);
}
}

void Save_Data(const string& filename, const vector<Investment>& data)
{
ofstream fout(filename);
if (!fout.is_open())
return;

fout << "NAME, BANK ACCOUNT, SORT CODE, INVESTMENT, Contribution\n";

for (auto& inv : data)
{
fout << inv.Name << " "
<< inv.BankAccount << " "
<< inv.SortCode << " "
<< inv.Investment << " "
<< inv.Contribution << "\n";
}
}

int main()
{
vector<Investment> Data_investment;
Get_Data("aaa.csv", Data_investment);

float total = accumulate(Data_investment.begin(), Data_investment.end(), 0.0f,[](float sum, const Investment& inv) { return sum + inv.Investment; }
);

for (auto& inv : Data_investment)
{
float percentage = (inv.Investment * 100.0f) / total;
inv.Contribution = percentage;
}

Save_Data("aaa_new.csv", Data_investment);

return 0;
}

最佳答案

我已经对您的源代码进行了很小的修改就成功地将其编译为Visual Studio 2017。请看一看。

#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cmath>
#include <numeric>

using namespace std;

struct Investment
{
std::string Name;
std::string BankAccount;
std::string SortCode;
float Invest; // Changed due the variable name goes same as struct name if use Investment
float Contribution;
};

string Trim(string &str)
{
str.erase(0, str.find_first_not_of(" \t\r\n"));
str.erase(str.find_last_not_of(" \t\r\n") + 1);
return str;
}

void Get_Data(const string& filename, vector<Investment>& data)
{
ifstream fin(filename);
if (!fin.is_open())
return;

string line;
if (!getline(fin, line))
return;

while (getline(fin, line))
{
istringstream sin(line);
vector<string> fields;
string field;

while (getline(sin, field, ','))
{
fields.push_back(field);
}

Investment inv;
inv.Name = Trim(fields[0]);
inv.BankAccount = Trim(fields[1]);
inv.SortCode = Trim(fields[2]);
inv.Invest = atof(Trim(fields[3]).c_str());
inv.Contribution = 0.0f;

data.push_back(inv);
}
}

void Save_Data(const string& filename, const vector<Investment>& data)
{
ofstream fout(filename);
if (!fout.is_open())
return;

fout << "NAME, BANK ACCOUNT, SORT CODE, INVESTMENT, Contribution\n";

for (auto& inv : data)
{
fout << inv.Name << " "
<< inv.BankAccount << " "
<< inv.SortCode << " "
<< inv.Invest << " "
<< inv.Contribution << "\n";
}
}

int main()
{
vector<Investment> Data_investment;
Get_Data("aaa.csv", Data_investment);

float total = accumulate(Data_investment.begin(), Data_investment.end(), 0.0f, [](float sum, const Investment& inv) { return sum + inv.Invest; }
);

for (auto& inv : Data_investment)
{
float percentage = (inv.Invest * 100.0f) / total;
inv.Contribution = percentage;
}

Save_Data("aaa_new.csv", Data_investment);

return 0;
}

下面是修改后的部分
std::string Name;
std::string BankAccount;
std::string SortCode;
float Invest; // Changed due the variable name goes same as struct name if use Investment

关于c++ - 编译以下代码时发生错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58634975/

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