【问题标题】:Array of Structs as Function Argument作为函数参数的结构数组
【发布时间】:2015-02-24 01:52:02
【问题描述】:

我有一个结构数组,我只想将它传递给函数以便对其进行排序。就将结构数组传递给函数而言,我做错了什么?原型、调用、定义有什么错误?

注意 - 我意识到我还没有初始化结构数组。在我的实际代码中,数组中包含来自文本文件的数据。这与我要问的问题无关。所以请不要评论数组中没有任何内容。

这是我无法开始工作的一小段代码示例:

#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);

}

此代码产生此错误:

错误 C2664: 'void bubbleSort(checkType,const int)' : 无法转换 参数 1 从 'checkType [5]' 到 'checkType'

然后我尝试将函数调用从bubbleSort(checkArray, NUM);更改为bubbleSort(checkArray[NUM], NUM);

此代码产生以下错误:

错误 LNK2019:无法解析的外部符号“void __cdecl bubbleSort(struct checkType,int)" (?bubbleSort@@YAXUcheckType@@H@Z) 在函数_main中引用

错误 LNK1120:1 个未解决的外部问题

谁能解释一下我做错了什么?

【问题讨论】:

  • 随便MY_STRUCT_TYPE **array

标签: c++ arrays struct


【解决方案1】:

转发声明:

void bubbleSort(checkType, const int);

定义:

void bubbleSort(checkType array[], const int SIZE)

那些不一样。前向声明应该是:

void bubbleSort(checkType[], const int);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多