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

时间:2014-03-29 07:25:15

标签: c++ function fstream

请假设我的代码示例已经存在标题。

我似乎无法使其发挥作用。

错误说:

  

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>'

这是我的作业问题:

  

问题:按键值对员工进行排序。

     

对于此作业,您将扩展您之前编写的员工薪资报表程序。应修改您的程序以允许输入最多15名员工的数据。无论输入员工数据的顺序如何,报表的详细信息行都应按升序员工姓名顺序打印。换句话说,您应该首先在生成报告之前对员工数据进行排序。您的程序应包括为之前的任务描述的Employee和Payroll Report类。

这是我的代码......

员工类:

    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;

    }

1 个答案:

答案 0 :(得分:2)

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

以下是如何将其缩减为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;
}

这更清楚地表明问题是Employee复制构造函数被隐式删除,因为infile有一个已删除的复制构造函数。此副本在Employee::swap()中调用。

请参阅using fstream object created as class member以获取解释。

似乎解决此问题的最佳方法是从infile删除Employee的所有知识(文件处理应该由Employee类负责吗?)。

我会删除int Employee::input()并将其替换为operator >>的{​​{1}}重载来完成此操作,例如:

Employee

然后,您需要更改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()