2014-02-24 36 views
-1
struct PayInfo 
{ 
    int hours; 
    double payRate; 
}; 

struct PayRoll 
{ 
    PayRoll(); 
    int empNumber; 
    string name; 
    double grossPay; 

    PayInfo pay; 
}; 

void GrossPay(PayRoll employee[]) 
{ 
    for (int i = 0; i < 3; i++) 
    { 
     employee[i].grossPay = employee[i].pay.hours * 
          employee[i].pay.payRate; 
    } 
} 

int main() 
{ 
    PayRoll employee[3]; 

    for (int i = 0; i < 3; i++) 
    { 
     cout << "Enter the employee's number: " << endl; 
     cin >> employee[i].empNumber; 

     cout << "Enter the employee's name: " << endl; 
     cin.ignore(); 
     getline(cin, employee[i].name); 

     cout << "How many hours did the employee work?" << endl; 
     cin >> employee[i].pay.hours; 

     cout << "What is the employee's hourly pay rate?" << endl; 
     cin >> employee[i].pay.payRate; 
    } 

    GrossPay(employee); 

    for (int j = 0; j < 3; j++) 
    { 
     cout << "Here is the employee's payroll data:\n"; 
     cout << "name: " << employee[j].name << endl; 
     cout << "Number: " << employee[j].empNumber << endl; 
     cout << "hours worked: " << employee[j].pay.hours << endl; 
     cout << "Hourly pay rate: " << employee[j].pay.payRate << endl; 
     cout << fixed << showpoint << setprecision(2); 
     cout << "Gross Pay: $" << employee[j].grossPay << endl; 
    } 

    return 0; 
} 

錯誤,我不知道如何解決我的錯誤。C++與「主要表達

Error: expected primary-expression before 'employee'

,但除此之外,我真的不知道如何把一個構造函數嵌套結構。我也真的不知道如何與一個數組定義結構

* * *編輯

現在說

ERROR: undefined reference to 'PayRoll::PayRoll()'

+0

你想要那條線究竟做什麼? – Brian

+0

您未通過實例。 – hofmeister

+0

我想創建一個使用數組結構值的函數 – user3345335

回答

1

傳遞變量作爲函數參數,只需指定變量名:

GrossPay(employee); 
1
GrossPay(PayRoll employee[]);  //Problem is here. 

當作爲函數參數傳遞時,只能使用變量名:

GrossPay(employee);