为什么我得到一个“标识符”数组“未定义”编译错误?

时间:2013-05-06 15:56:52

标签: c++ arrays loops

每次我尝试运行下面的代码时,都会显示两个错误。两个错误都是“标识符”数组“在第43和56行都未定义。

可能导致此问题的原因以及如何解决?

#include <iostream>

using namespace std;

typedef double* arrayT;

double avg(arrayT data, int length);
double min(arrayT data, int length);
double max(arrayT data, int length);

int main()
{
    cout << "Please input the number of temperatures to be read." << endl;
    int num;
    cin >> num;
    arrayT temperatures = new double[num];

    for(int i = 0; i < num; i++)
    {
        cout << "Input temperature " << (i+1) << ":" << endl;
        cin >> temperatures[i];
    }

    cout << "The average temperature is " << avg(temperatures, num) << endl;
    cout << "The highest temperature is " << max(temperatures, num) << endl;
    cout << "The lowest temperature is " << min(temperatures, num) << endl;
}

double avg(arrayT data, int length)
{
    double sum = 0.0;

    for(int i = 0; i < length; i++)
        sum += data[i];
    return sum/length;
}

double min(arrayT data, int length)
{
    if(length == 0)
        return 0;

    double min = array[0];

    for(int i = 1; i < length; i++)
        if(array[i] < min)
            min = array[i];
    return min;
}

double max(arrayT data, int length)
{
    if(length == 0)
        return 0;

    double max = array[0];

    for(int i = 1; i < length; i++)
        if(array[i] > min)
            max = array[i];
    return max;
}

1 个答案:

答案 0 :(得分:3)

在min和max函数中,您有名为'data'的参数,但在函数体中,您可以通过'array'调用它们。

double min(arrayT data, int length)
{
..
// here
    double min = array[0];

..
}

double max(arrayT data, int length)
{
..
// here
    double max = array[0];
..
}