我不明白为什么这不会运行。我应该将hours * payrate
存储在工资数组中,然后showResults
获取此信息并cout
。我运行时得到的错误是
error LNK2019: unresolved external symbol "void __cdecl showResults(int * const,int,double * const,double * const,double * const)" (?showResults@@YAXQAHHQAN11@Z) referenced in function
error LNK2019: unresolved external symbol "void __cdecl getEmployeeData(int * const,int,double * const,double * const,double * const)" (?getEmployeeData@@YAXQAHHQAN11@Z) referenced in function `_main`
代码如下:
#include <iomanip>
#include <iostream>
using namespace std;
void getEmployeeData(int[], int, double[], double[], double[]);
void showResults(int[], int, double[], double[], double[]);
int main()
{
const int ARRAY_SIZE = 7;
int empId[ARRAY_SIZE] = {565, 845, 452, 130, 789, 758, 877};
double hoursWorked[ARRAY_SIZE]; // Holds hours worked
double payrate[ARRAY_SIZE]; // Holds pay rate
double wages[ARRAY_SIZE]; // Holds wages
getEmployeeData(empId, ARRAY_SIZE, payrate, hoursWorked, wages);
showResults(empId, ARRAY_SIZE, payrate, hoursWorked, wages);
system("pause");
return 0;
}
void getEmployeedata(int nums[],int size,double pay[],double hours[],
double wages[])
{
//Hours worked and Pay rate
for(int index = 0; index < size; index++)
{
cout << "Enter number of hours worked by employee number "
<< nums[index] << ": ";
cin >> hours[index];
cout << "\nEnter hourly pay rate ";
cin >> pay[index];
wages[index] = hours[index] * pay[index];
}
}
void showResults(int nums[], int size, double pay, double hours, double wages[])
{
for(int index = 0; index < size; index++)
{
cout << "Employee Number Gross Wage " << endl
<< nums[index] << " " << wages[index];
}
}
最佳答案
showResults
的声明和定义不同意参数类型。特别是,pay
和 hours
参数在声明中是 double[]
但在定义中只是 double
。
void showResults(int[], int, double[], double[], double[]) // Declaration
// ↕↕ ↕↕
void showResults(int[], int, double , double , double[]) // Definition
看起来声明是正确的,而定义是错误的,因为您将数组传递给它们。
您对 getEmployeeData
的声明和定义不同意名称。
void getEmployeeData(int[], int, double[], double[], double[]) // Declaration
/ ↕
void getEmployeedata(int[], int, double[], double[], double[]) // Definition
您可能希望 getEmployeeData
与其余命名保持一致。
关于c++通过函数传递数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15854380/