结构数组作为函数参数
Array of Structs as Function Argument
我有一个结构数组,我只想将它传递给函数以便对其进行排序。就将结构数组传递给函数而言,我做错了什么?原型、调用、定义有哪些错误?
注意 - 我意识到我还没有初始化结构数组。在我的实际代码中,该数组包含来自文本文件的数据。这与我要问的问题无关。所以请不要评论数组中没有任何内容。
这是我无法开始工作的一小段代码示例:
#include <iostream>
using namespace std;
struct checkType
{
char date[12];
char checkNum[8];
float amount;
};
void bubbleSort(checkType, const int);
int main()
{
const int NUM = 5;
checkType checkArray[NUM];
bubbleSort(checkArray, NUM);
return 0;
}
void bubbleSort(checkType array[], const int SIZE)
{
bool swap;
checkType temp;
do
{
swap = false;
for (int count = 0; count < (SIZE - 1); count++)
{
if (strcmp(array[count].date, array[count + 1].date) > 0)
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
此代码产生此错误:
error C2664: 'void bubbleSort(checkType,const int)' : cannot convert
argument 1 from 'checkType [5]' to 'checkType'
然后我尝试将函数调用从 bubbleSort(checkArray, NUM);
更改为 bubbleSort(checkArray[NUM], NUM);
此代码产生以下错误:
error LNK2019: unresolved external symbol "void __cdecl
bubbleSort(struct checkType,int)" (?bubbleSort@@YAXUcheckType@@H@Z)
referenced in function _main
error LNK1120: 1 unresolved externals
有人可以解释一下我做错了什么吗?
转发声明:
void bubbleSort(checkType, const int);
定义:
void bubbleSort(checkType array[], const int SIZE)
它们不一样。前向声明应该是:
void bubbleSort(checkType[], const int);
我有一个结构数组,我只想将它传递给函数以便对其进行排序。就将结构数组传递给函数而言,我做错了什么?原型、调用、定义有哪些错误?
注意 - 我意识到我还没有初始化结构数组。在我的实际代码中,该数组包含来自文本文件的数据。这与我要问的问题无关。所以请不要评论数组中没有任何内容。
这是我无法开始工作的一小段代码示例:
#include <iostream>
using namespace std;
struct checkType
{
char date[12];
char checkNum[8];
float amount;
};
void bubbleSort(checkType, const int);
int main()
{
const int NUM = 5;
checkType checkArray[NUM];
bubbleSort(checkArray, NUM);
return 0;
}
void bubbleSort(checkType array[], const int SIZE)
{
bool swap;
checkType temp;
do
{
swap = false;
for (int count = 0; count < (SIZE - 1); count++)
{
if (strcmp(array[count].date, array[count + 1].date) > 0)
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
此代码产生此错误:
error C2664: 'void bubbleSort(checkType,const int)' : cannot convert argument 1 from 'checkType [5]' to 'checkType'
然后我尝试将函数调用从 bubbleSort(checkArray, NUM);
更改为 bubbleSort(checkArray[NUM], NUM);
此代码产生以下错误:
error LNK2019: unresolved external symbol "void __cdecl bubbleSort(struct checkType,int)" (?bubbleSort@@YAXUcheckType@@H@Z) referenced in function _main
error LNK1120: 1 unresolved externals
有人可以解释一下我做错了什么吗?
转发声明:
void bubbleSort(checkType, const int);
定义:
void bubbleSort(checkType array[], const int SIZE)
它们不一样。前向声明应该是:
void bubbleSort(checkType[], const int);