c++/error: no matching function for call to calcGrossWages()
c++/error: no matching function for call to calcGrossWages()
/**************************************************************************************
* The program should display each employee number *
* ask the user to enter that employee’s hours and pay rate. *
* It should then calculate the gross wages for that employee(hours times pay rate) *
* store them in the wages array. *
* After the data has been entered for all the employees, *
* the program should display each employee’s identification number and gross wages. *
**************************************************************************************/
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
const int Num_Employees = 7; // global constant of # of employees
int empId[Num_Employees] = {5658846, 4520125, 785122, 877541, 8451277, 1302850, 7580489}; // array of Employee ID #'s
int hours[7]; // empty array of 7 possible values for employee hours
double payRate[7]; // empty array of 7 possible values for employee pay rates
double wages[7]; // empty array of 7 possible values for employees wages (hours * pay rate)
void calcGrossWages(int[], double[], double[]); // calculate gross wages prototype
int main() {
// Employees
for(int i= 0; i< Num_Employees; i++) {
cout << "Your ID is: " << "" << empId[i] << endl; // displays each employee #
cout << "How many hours did you work?";
cin >> hours[i];
cout << "What was your payRate?" << endl;
cout <<"$";
cin >> payRate[i];
}
/*Calculate the gross wages*/
for(int i = 0; i < Num_Employees; i++) {
wages[i] = calcGrossWages(hours[i], payRate[i], wages[i]);
}
}
//******************************************************************************
//* Definition of calcGrossWages function *
//* This function calculates the employees Wages *
//* Wages are calculated by the # of hours worked by each employee *
//* multiplied by their enter pay rate *
// *****************************************************************************
void calcGrossWages(int hours[], double payRate[], double wages[])
{
for (int i= 0; i< Num_Employees; i++) {
wages[i] = hours[i] * payRate[i];
}
}
问题:
如何正确地将数组作为参数传递给函数,从而允许在空数组中输入值?
为什么我会收到一条错误消息,提示没有匹配的调用函数 'calcGrossWages'
array[i]
是一个数组元素,不是整个数组;您正在尝试传递 int
s 和 double
s 而不是它们的数组。
由于数组的名称是 hours
、payRate
和 wages
,您可以这样调用函数:
/*Calculate the gross wages*/
calcGrossWages(hours, payRate, wages)
您不需要循环,因为该函数已经处理了整个数组,并且该函数没有 return 任何东西(结果存储在 wages
中)。
(附带说明一下,这些变量应该是 main
的局部变量,而不是全局变量。)
您应该在 calcGrossWages 中传递数组,而不是仅仅传递数组元素,并且不需要在 main 中循环调用 calcGrossWages
/******************************************************************************** ******
* The program should display each employee number *
* ask the user to enter that employee’s hours and pay rate. *
* It should then calculate the gross wages for that employee(hours times pay rate) *
* store them in the wages array. *
* After the data has been entered for all the employees, *
* the program should display each employee’s identification number and gross wages. *
******************************************************************************** ******/
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
const int Num_Employees = 7; // global constant of # of employees
int empId[Num_Employees] = {5658846, 4520125, 785122, 877541, 8451277, 1302850, 7580489}; // array of Employee ID #'s
int hours[7]; // empty array of 7 possible values for employee hours
double payRate[7]; // empty array of 7 possible values for employee pay rates
double wages[7]; // empty array of 7 possible values for employees wages ( hours * pay rate)
void calcGrossWages(int[], double[], double[]); // calculate gross wages prototype
int main() {
// Employees
for(int i= 0; i< Num_Employees; i++) {
cout << "Your ID is: " << "" << empId[i] << endl; // displays each employee #
cout << "How many hours did you work?";
cin >> hours[i];
cout << "What was your payRate?" << endl;
cout <<"$";
cin >> payRate[i];
}
/*Calculate the gross wages*/
calcGrossWages(hours, payRate, wages);
}
//******************************************************************************
//* Definition of calcGrossWages function *
//* This function calculates the employees Wages *
//* Wages are calculated by the # of hours worked by each employee *
//* multiplied by their enter pay rate *
// *****************************************************************************
void calcGrossWages(int hours[], double payRate[], double wages[])
{
for (int i= 0; i< Num_Employees; i++) {
wages[i] = hours[i] * payRate[i];
}
}
/**************************************************************************************
* The program should display each employee number *
* ask the user to enter that employee’s hours and pay rate. *
* It should then calculate the gross wages for that employee(hours times pay rate) *
* store them in the wages array. *
* After the data has been entered for all the employees, *
* the program should display each employee’s identification number and gross wages. *
**************************************************************************************/
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
const int Num_Employees = 7; // global constant of # of employees
int empId[Num_Employees] = {5658846, 4520125, 785122, 877541, 8451277, 1302850, 7580489}; // array of Employee ID #'s
int hours[7]; // empty array of 7 possible values for employee hours
double payRate[7]; // empty array of 7 possible values for employee pay rates
double wages[7]; // empty array of 7 possible values for employees wages (hours * pay rate)
void calcGrossWages(int[], double[], double[]); // calculate gross wages prototype
int main() {
// Employees
for(int i= 0; i< Num_Employees; i++) {
cout << "Your ID is: " << "" << empId[i] << endl; // displays each employee #
cout << "How many hours did you work?";
cin >> hours[i];
cout << "What was your payRate?" << endl;
cout <<"$";
cin >> payRate[i];
}
/*Calculate the gross wages*/
for(int i = 0; i < Num_Employees; i++) {
wages[i] = calcGrossWages(hours[i], payRate[i], wages[i]);
}
}
//******************************************************************************
//* Definition of calcGrossWages function *
//* This function calculates the employees Wages *
//* Wages are calculated by the # of hours worked by each employee *
//* multiplied by their enter pay rate *
// *****************************************************************************
void calcGrossWages(int hours[], double payRate[], double wages[])
{
for (int i= 0; i< Num_Employees; i++) {
wages[i] = hours[i] * payRate[i];
}
}
问题:
如何正确地将数组作为参数传递给函数,从而允许在空数组中输入值?
为什么我会收到一条错误消息,提示没有匹配的调用函数 'calcGrossWages'
array[i]
是一个数组元素,不是整个数组;您正在尝试传递 int
s 和 double
s 而不是它们的数组。
由于数组的名称是 hours
、payRate
和 wages
,您可以这样调用函数:
/*Calculate the gross wages*/
calcGrossWages(hours, payRate, wages)
您不需要循环,因为该函数已经处理了整个数组,并且该函数没有 return 任何东西(结果存储在 wages
中)。
(附带说明一下,这些变量应该是 main
的局部变量,而不是全局变量。)
您应该在 calcGrossWages 中传递数组,而不是仅仅传递数组元素,并且不需要在 main 中循环调用 calcGrossWages
/******************************************************************************** ******
* The program should display each employee number *
* ask the user to enter that employee’s hours and pay rate. *
* It should then calculate the gross wages for that employee(hours times pay rate) *
* store them in the wages array. *
* After the data has been entered for all the employees, *
* the program should display each employee’s identification number and gross wages. *
******************************************************************************** ******/
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
const int Num_Employees = 7; // global constant of # of employees
int empId[Num_Employees] = {5658846, 4520125, 785122, 877541, 8451277, 1302850, 7580489}; // array of Employee ID #'s
int hours[7]; // empty array of 7 possible values for employee hours
double payRate[7]; // empty array of 7 possible values for employee pay rates
double wages[7]; // empty array of 7 possible values for employees wages ( hours * pay rate)
void calcGrossWages(int[], double[], double[]); // calculate gross wages prototype
int main() {
// Employees
for(int i= 0; i< Num_Employees; i++) {
cout << "Your ID is: " << "" << empId[i] << endl; // displays each employee #
cout << "How many hours did you work?";
cin >> hours[i];
cout << "What was your payRate?" << endl;
cout <<"$";
cin >> payRate[i];
}
/*Calculate the gross wages*/
calcGrossWages(hours, payRate, wages);
}
//******************************************************************************
//* Definition of calcGrossWages function *
//* This function calculates the employees Wages *
//* Wages are calculated by the # of hours worked by each employee *
//* multiplied by their enter pay rate *
// *****************************************************************************
void calcGrossWages(int hours[], double payRate[], double wages[])
{
for (int i= 0; i< Num_Employees; i++) {
wages[i] = hours[i] * payRate[i];
}
}