c++通过函数传递数组

标签 c++ arrays function

我不明白为什么这不会运行。我应该将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 的声明和定义不同意参数类型。特别是,payhours 参数在声明中是 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/

相关文章:

C++ 构建系统

c - 我需要在数组中存储一些整数并用字母符号打印它们

python - 在python中用字典值划分列表元素

arrays - Hive Array<Struct<>>插入显示null

java - 检查 EditText 内容中的特定字符串

c++ - Qt:单实例应用保护的最佳实践

c++ - 段错误(核心转储) - 使用大小为 8 的未初始化值

php - 为什么类名不回退到命名空间中的全局类名?

C++ struct 默认构造函数行为

c++ - 函数指针的可变函数