我的班级函数调用错误

时间:2014-10-22 02:21:51

标签: c++

该程序应该提示用户提供两名不同的员工,并打印他们的名字,姓氏,ssn,payrate和本周的总工资。此外,它应确定工人是否在任何时间工作,并重新计算他​​们的工资。

我不断收到此错误消息:

No matching function call to Employee::Employee() 

我将使用// *****标记此消息显示的行。

信息出现3次。请检查我的问题并帮助我解决问题,并解释您为修复它们所采取的措施。

#include <cstdlib>
#include <iostream>
#include <math.h>

/*
Name: Employee2
Author: --------
Date: 20/10/14 20:36
Description: A program that prints workers info using classes
*/
using namespace std;

class Employee
{
private:
    string firstname, lastname, ssn;
    int payRate, hours;

public:

    //A Four Parameter Constructor

    Employee (string newFirst, string newLast, string newSsn, int newpayRate, int newHours)
    {
        firstname = newFirst;
        lastname = newLast;
        ssn = newSsn;
        payRate = newpayRate;
        hours = newHours;
    }

    //Setter or Mutator Functions

    void setnewFirst(string newFirst)
    {
        firstname = newFirst;
    }

    void setnewLast(string newLast)
    {
        lastname = newLast;
    }

    void setnewSsn(string newSsn)
    {
        ssn = newSsn;
    }

    void setnewpayRate(int newpayRate)
    {
        payRate = newpayRate;
    }

    void setnewHours(int newHours)
    {
        hours = newHours;
    }

    void setEmployee(string newFirst, string newLast, string newSsn, int newpayRate, int     newHours)
    {
        firstname = newFirst;
        lastname = newLast;
        ssn = newSsn;
        payRate = newpayRate;
        hours = newHours;
    }

    //Accessor Functions

    string getfirstname ()
    {
        return firstname;
    }

    string getlastname ()
    {
        return lastname;
    }

    string getssn ()
    {
        return ssn;
    }

    int getpayRate ()
    {
        return payRate;
    }

    int gethours ()
    {
        return hours;
    }

    //Output Functions

    void printEmployee ()
    {
        cout << firstname << " " << lastname << endl << ssn << endl << payRate << endl <<   hours << endl;
    }

    //Functions to use employee info

    Employee newEmployee ()
    {
        Employee e1;      //**************
        string newFirst;
        string newLast;
        string newSsn;
        int newpayRate;
        int newHours;
        cout << "Enter First Name: " ;
        cin >> newFirst;
        cout << "Enter Last Name: " ;
        cin >> newLast;
        cout << "Enter SSN: " ;
        cin >> newSsn;
        cout << "Enter Payrate: " ;
        cin >> newpayRate;
        cout << "Enter Hours Worked: " ;
        cin >> newHours;
        e1.setnewFirst(newFirst);
        e1.setnewLast(newLast);
        e1.setnewSsn(newSsn);
        e1.setnewpayRate(newpayRate);
        e1.setnewHours(newHours);
        return e1;
    }

    //Function to Calculate Weekly Pay

    int calculatePay (int hours)
    {
        double result;
        if ( hours > 40 )
        {
            result = (hours - 40) * (payRate * 1.5) + (40 * payRate);
        }
        else
        {
            result = (hours * payRate);
        }
    }
};

Employee newEmployee();

//Main

int main(int argc, char *argv[])
{
    Employee firstEmployee;    // *********************
    Employee secondEmployee;   // *********************
    double result;
    firstEmployee = firstEmployee.newEmployee();
    secondEmployee = secondEmployee.newEmployee();
    cout << "First Employee Pay: " ;
    firstEmployee.printEmployee();
    cout << endl;
    cout << "Secnod Employee Pay: " ;
    secondEmployee.printEmployee();

    system("PAUSE");
    return EXIT_SUCCESS;
}

1 个答案:

答案 0 :(得分:0)

构造函数旨在对创建新对象的空间进行操作。这意味着您不需要通过创建附加对象e1来使事情复杂化。删除有问题的行:

Employee e1;      //**************

从代码中删除所有e1引用;例如改变

e1.setnewFirst(newFirst);
e1.setnewLast(newLast);
e1.setnewSsn(newSsn);
e1.setnewpayRate(newpayRate);
e1.setnewHours(newHours);

为:

setnewFirst(newFirst);
setnewLast(newLast);
setnewSsn(newSsn);
setnewpayRate(newpayRate);
setnewHours(newHours);

构造函数不返回值;删除:

    return e1;

编译,测试,调试,冲洗和重复。