2014-10-22 51 views
-1

此程序應提示用戶提供兩名不同的員工,並列出他們的姓名,姓氏,ssn,支付率和本週的總工資。還應該確定工人是否隨時工作並重新計算他​​們的工資。我的班級發生函數調用錯誤

我不斷收到此錯誤信息:

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; 
} 

回答

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; 

編譯,測試,調試,漂洗和重複。

相關問題