c++通过函数传递数组
c++ passing arrays through functions
我不明白为什么这不会运行。我应该将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
与您的其余命名保持一致。
如果您想将数组传递给函数,也可以尝试将函数写成
void getEmployeeData(int*, int, double*, double*, double*);
void showResults(int*, int, double*, double*, double*);
因为当你传递数组时,你传递的是指向该数组第0个下标的指针。另外,如前所述,您的代码中有一个错别字,请将getEmployeedata
替换为getEmployeeData
下面是更正后的代码:
#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[7] = {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];
}
}
你的函数名有拼写错误:
getEmployeeData(empId, ARRAY_SIZE, payrate, hoursWorked, wages);
然而,在定义中你有:
getEmployeedata(...);
也不匹配:
void showResults(int nums[], int size, double pay, double hours, double wages[])
//^^^^^^^^^^^^^^^^^
既然您确切地知道每个数组有多少个元素,您可以尝试使ARRAY_SIZE
成为全局常量(因此它与您的函数原型处于相同的作用域),允许您做这样的事情:
void getEmployeeData(int (&)[ARRAY_SIZE], int, double (&)[ARRAY_SIZE], double (&)[ARRAY_SIZE], double (&)[ARRAY_SIZE]);
void showResults(int (&)[ARRAY_SIZE], int, double (&)[ARRAY_SIZE], double (&)[ARRAY_SIZE], double (&)[ARRAY_SIZE]);
void getEmployeeData(int (&nums)[ARRAY_SIZE], int size, double (&pay)[ARRAY_SIZE], double (&hours)[ARRAY_SIZE], double (&wages)[ARRAY_SIZE]) {
// Function body
}
void showResults(int (&nums)[ARRAY_SIZE], int size, double (&pay)[ARRAY_SIZE], double (&hours)[ARRAY_SIZE], double (&wages)[ARRAY_SIZE]) {
// Function body
}
现在,这看起来很尴尬,但它的作用是告诉编译器,该函数接受对的数组的引用,这些数组恰好是 ARRAY_SIZE
元素。(具体来说,[]
具有比&
更高的优先级,因此您需要括号告诉编译器该参数是对数组的引用,而不是对引用数组的引用。)它还允许您使用ol' C sizeof(nums)/sizeof(nums[0])
来确定数组的大小;不像让数组衰变成一个指针(在这种情况下,sizeof
会给你无用的信息,因为它把nums
看作是int*
而不是int[ARRAY_SIZE]
),通过引用传递它保留了这些信息。
或者,如果你可以自由地重写代码,并且你的编译器支持c++ 11,你可以使用std::array
来代替C风格的数组。
#include <array>
using namespace std;
void getEmployeeData(array<int, ARRAY_SIZE>, int, array<double, ARRAY_SIZE>, array<double, ARRAY_SIZE>, array<double, ARRAY_SIZE>);
void showResults(array<int, ARRAY_SIZE>, int, array<double, ARRAY_SIZE>, array<double, ARRAY_SIZE>, array<double, ARRAY_SIZE>);
array<int, ARRAY_SIZE> empId = {565, 845, 452, 130, 789, 758, 877};
array<double, ARRAY_SIZE> hoursWorked; // Holds hours worked
array<double, ARRAY_SIZE> payrate; // Holds pay rate
array<double, ARRAY_SIZE> wages; // Holds wages
作为一个容器,std::array
可以与c风格的数组(即operator[]
)使用相同的语法,所以你不需要重写任何实际使用数组的代码。它还允许getEmployeeData()
和showResults()
直接检查数组的大小,如nums.size()
。
还请注意,如果您使用其中任何一个,实际上不需要传递int size
,因为您可以使用sizeof
或std::array.size()
。这确实有污染全局命名空间的缺点,但我个人认为这是一个公平的权衡,以保证您的代码仅在函数被给定适当大小的数组时才能编译。
[注意,如果你需要能够在不同的时间传递不同大小的数组,这两种方法对你来说都没有用。]
我为我可能错过的任何类型道歉。
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 打印 2D 动态数组 c++ 函数
- C++数组函数
- 在数组函数指针中调用void
- 数组函数似乎没有找到最大值
- 使用数组函数和循环求解伪元编码|C
- 在头文件中的函数原型中获取指针数组:函数声明为 void
- 数组函数错误输出
- 读取文件,然后创建数组函数C++
- 指针数组函数参数-奇怪的行为
- 二维数组 - 函数
- "return"没有从数组函数返回正确的值
- 数组函数问题
- 数组函数的大小返回 int 的大小而不是数组
- 从数组函数中删除元素
- 如何从C++中最好地调用具有C99风格数组函数签名的函数
- 追加/打印/插入数组c++函数问题
- 如何将数组函数转换为指针函数
- 多维数组函数输出垃圾
- C++:数组函数的递归最大值