我是计算机科学第一年学习C ++。我们有一项任务,必须从文件中加载数据,使用构造函数并将此数据保存在Vector中。
我已经仔细阅读了我们的讲义,并且没有如何执行此操作的示例,并且我已经在Google上搜索了数小时,却一无所获。
也许我问错了什么?
由于我尚未对其进行研究,因此我暂时将所有其他类和函数都省略了。
这是我到目前为止管理的代码:
#pragma once
#include<iostream>
#include<string>
#include<fstream>
#include<vector>
using namespace std;
class OrganisationRecord
{
private:
public:
string name;
string occupation;
string department;
};
class payrollprocessing
{
private:
ifstream inputfile;
ofstream outputfile;
vector<OrganisationRecord> OrganisationRecords; //Vector it needs to be saved too
vector<HRRecord> HRRecords;
vector<PayRollRecord> PayRollRecords;
public:
void loadOrganisationRecords(string filename); //Function I am trying to write
void loadHRRecords(string filename);
void loadPayrollRecords(string filename);
void displayEmployeeOfSalaryGTE(double salary);
//GTE = Greater than or equal to
};
void payrollprocessing::loadOrganisationRecords(string name)
{
inputfile.open("organisation.txt");
if (!inputfile.is_open())
{
cout << "File Not Open Error!";
}
else
{
std::string line;
int i = 0;
while (!inputfile.eof())
{
OrganisationRecord OrgRec; //Here is the code to
inputfile >> OrgRec.name; //to construct and then
inputfile >> OrgRec.occupation; //save to the vector
inputfile >> OrgRec.department; //we havent learned how to
//how to print string
OrganisationRecords.push_back(OrgRec); //vectors yet which makes
i++; //it hard to test
}
}
inputfile.close();
}
int main(void)
{
string name;
payrollprocessing PP;
PP.loadOrganisationRecords(name);
return 0;
}
我们也无法编辑库或类。
非常感谢所有帮助!
最佳答案
您的类未定义构造函数。添加一个构造函数以调用您的loadOrganisationRecords函数。我怀疑您的搜索过于具体。尝试自己搜索“ C ++构造函数”,您将获得大量信息。
看起来您对文件I / O和向量使用有正确的想法,但是单独搜索它们也不是一个坏主意。
关于c++ - 类函数,用于从文件中获取数据并从另一个类构造,以将其保存在Vector C++中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41143045/