gpt4 book ai didi

C++作业。按顺序排列工资单中的姓名。使用数组

转载 作者:行者123 更新时间:2023-11-28 02:52:29 25 4
gpt4 key购买 nike

请假定 header 已在我的代码示例中准备就绪。

我似乎无法正常工作。

错误说:

c:\program files (x86)\microsoft visual studio 10.0\vc\include\fstream(890): error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

这是我的作业问题:

Question: Sorting Employees by a key value.

For this assignment you are to extend the Employee Payroll Report program you wrote previously. Your program should be modified to allow data for up to 15 employees to be entered. Regardless of which order the employee data is entered, the detail lines of the report should be printed in ascending employee name order. In other words, you should first sort the employee data before generating report. Your program should include the Employee and Payroll Report classes that were described for the previous assignment.

这是我的代码...

员工类:

    class Employee 
{
public:
Employee();
int input();
void calculate();
void display();
void input_storage(Employee []);
void sort(Employee[], int);
void swap(Employee &, Employee &);
void set_name(string);
string get_name();
void set_hours(double);
double get_hours();
void set_rate(double);
double get_rate();
void set_ovrhours(double);
double get_ovrhours();
void set_sal(double);
double get_sal();
void set_ovrsal(double);
double get_ovrsal();
void set_count(int);
int get_count();

private:
string name;
int count;
double hours, rate, ovrhours, sal, ovrsal;
ifstream infile;

};

Employee::Employee()
{
count=0;
infile.open("payroll.txt", ios::in);
if (!infile)
cout << " Error: Could not open input file. \n";
}

void Employee::set_name(string new_name)
{
name=new_name;
}

void Employee::set_hours(double new_hours)
{
hours=new_hours;
}

void Employee::set_rate(double new_rate)
{
rate=new_rate;
}

void Employee::set_sal(double new_sal)
{
sal=new_sal;
}

void Employee::set_ovrsal(double new_ovrsal)
{
ovrsal=new_ovrsal;
}

void Employee::set_ovrhours(double new_ovrhours)
{
ovrhours=new_ovrhours;
}


int Employee::input()
{
if (infile >> name >> hours >> rate)
return 1;
else
return 0;
}

void Employee::calculate()
{
ovrhours=hours-40;

if (ovrhours>=10)
{
ovrsal=(10*(1.5*rate))+((hours-50)*(2*rate));
sal=(40*rate)+ovrsal;
}

else if (ovrhours>0 && ovrhours<10)
{
ovrsal=ovrhours*(rate*1.5);
sal=(40*rate)+ovrsal;
}

else
{
ovrhours=0.0;
ovrsal=0.0;
sal=hours*rate;
}
}

void Employee::input_storage(Employee storage[])
{
storage[count].name=name;
storage[count].rate=rate;
storage[count].hours=hours;
storage[count].ovrhours=ovrhours;
storage[count].ovrsal=ovrsal;
storage[count].sal=sal;
count++;
}

void Employee::sort(Employee storage[], int n)
{
n=count;
for (int i=n-1; i>0; i--)
for (int j=0; j<i; j++)
if (storage[j].name > storage[j+1].name) swap(storage[j],storage[j+1]);
}

void Employee::swap(Employee &a, Employee &b)
{
Employee temp = a;
a = b;
b = temp;
}



void Employee::display()
{
cout << "Report is created in your respective workspace." << endl;

}

string Employee::get_name()
{
return name;
}

double Employee::get_rate()
{
return rate;
}

double Employee::get_ovrhours()
{
return ovrhours;
}

double Employee::get_hours()
{
return hours;
}
double Employee::get_sal()
{
return sal;
}
double Employee::get_ovrsal()
{
return ovrsal;
}

工资报表类:

    class PayrollReport
{
public:
PayrollReport();
void insertDetail(Employee [], int);
void border(int);
void display_total(Employee[]);
void columnheadings();
void indent(int);
void reportheader();
void skiplines(int);

private:
double tot_hours, tot_ovrhours, tot_ovrsal, tot_sal;
ofstream outreport;
};


PayrollReport::PayrollReport()
{
outreport.open("report.txt", ios::out);
tot_hours=0.0;
tot_ovrhours=0.0;
tot_ovrsal=0.0;
tot_sal=0.0;

}

void PayrollReport::indent(int n)
{
for (int i=0; i<n; i++)
outreport << " ";
}
void PayrollReport::skiplines (int n)
{
for (int i=1; i<=n; i++)
outreport << endl;
}

void PayrollReport::reportheader()
{
border(100);
skiplines(1);
indent(37);
outreport << "EMPLOYEE PAYROLL REPORT"<<endl;
border(100);
}


void PayrollReport::columnheadings()
{
outreport << endl<<setiosflags(ios::right)
<< setw (15) << ' ' << setw (10) << "Hourly"
<< setw(10) <<" " << setw(10) << "Overtime"
<< setw(10) << "Overtime" << setw(10) <<"Total" << endl
<< setiosflags(ios::left) << setw (15) << "Name"
<< setiosflags(ios::right)
<< setw (10) << "Rate" << setw(10) << "Hours" << setw(10) << "Hours"
<< setw(10) << "Salary" << setw(10) <<"Salary" << endl;
border(100);
skiplines(1);
}


void PayrollReport::insertDetail(Employee storage[], int n)
{
outreport << setiosflags(ios::right)<<setprecision(2) << std::fixed <<setw(15)<< storage[n].get_name() << ' ' <<
setw(10)<<storage[n].get_hours()<< setw(10) << storage[n].get_rate() << setw(10)<< storage[n].get_ovrhours() << setw(10)
<< storage[n].get_ovrsal() << setw(10) << storage[n].get_sal() << endl;


tot_hours+=storage[n].get_hours();
tot_sal+=storage[n].get_sal();
tot_ovrhours+=storage[n].get_ovrhours();
tot_ovrsal+=storage[n].get_ovrsal();

}

void PayrollReport::display_total(Employee[])
{
border(100);
skiplines(1);
outreport << setiosflags(ios::showpoint | ios::fixed | ios::right)
<< setw(15)<<"Totals"<<setw(22) << tot_hours
<< setw(10) << tot_ovrhours
<< setw(10) << tot_ovrsal
<< setw(10) << tot_sal << endl;

skiplines(4);
border(100);
skiplines(1);
border(100);

}

void PayrollReport::border(int n)
{
for (int i=0; i<n; i++)
outreport << "-";
}

主要功能:

    int main()
{

int loopcount;
Employee x;
Employee storage[15];
PayrollReport p;

p.reportheader();
p.columnheadings();


while (x.input()==1)
{
x.calculate();
x.input_storage(storage);

loopcount++;
}

x.sort(storage, loopcount);

for (int i=0; i<loopcount; i++)
p.insertDetail(storage, loopcount);


p.display_total(storage);
x.display();

cin.ignore();
return 0;

}

最佳答案

那里有很多与实际问题无关的代码!

以下是如何将其缩减为 SSCCE 的方法使我们更容易为您提供帮助:

#include <iostream>
#include <fstream>

using namespace std;

class Employee
{
public:
Employee();
void swap(Employee &, Employee &);
private:
int count;
ifstream infile;
};

Employee::Employee()
{
count=0;
infile.open("payroll.txt", ios::in);
if (!infile)
cout << " Error: Could not open input file. \n";
}

void Employee::swap(Employee &a, Employee &b)
{
Employee temp = a;
a = b;
b = temp;
}

这更清楚地表明问题是 Employeecopy constructor 被隐式删除,因为 infile 有一个已删除的copy constructor。此拷贝在 Employee::swap() 中调用。

参见 using fstream object created as class member寻求解释。

似乎解决这个问题的最好方法是从 Employee 中删除所有关于 infile 的知识(文件处理应该由 Employee 类?)。

我会通过删除 int Employee::input() 并将其替换为 Employeeoperator>> 重载来实现,一些喜欢:

std::ifstream& operator >>(std::ifstream& is, Employee& employee)
{
std::string name;
double hours, rate;
if (is >> name >> hours >> rate)
{
employee.set_name(name);
employee.set_hours(hours);
employee.set_rate(rate);
}
return is;
}

然后您需要在 main() 函数中更改打开和读取文件的方式:

// [...]
p.columnheadings();

std::ifstream infile("payroll.txt");
if (!infile)
{
// N.B: Report errors on stderr!
std::cerr << " Error: Could not open input file. \n";
}

while (infile >> x)
{
x.calculate();
x.input_storage(storage);

loopcount++;
}

x.sort(storage, loopcount);
// [...]

关于C++作业。按顺序排列工资单中的姓名。使用数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22728268/

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