【问题标题】:Why am i getting this compile error when i try to compile?为什么我在尝试编译时会出现此编译错误?
【发布时间】:2013-08-14 13:03:27
【问题描述】:

我对 C++ 编程有点陌生,我被分配了一个练习,我得到了一个编译错误

我希望有人可以帮助我解决错误或让我了解错误发生的原因 下面的代码 /* 练习 21 中级:声明一个 7 行 2 列的 int 数组,命名为 temperature。 程序应提示用户输入 7 天的最高和最低温度。 将最高温度存储在数组的第一列中。 将最低温度存储在第二列中。 程序应显示平均高温和平均低温。 显示平均温度,保留一位小数。 */

#include <iostream>
#include <iomanip>
using namespace std;

//function prototype
void calcAverage(double temperatures[7][2]);

main()
{
double temperatures[7][2] = {0};

float high = 0.0;
float low = 0.0;
double high_average = 0.0;
double low_average = 0.0;



cout << "Please enter the high then low for the last 7 days " <<endl;

for(int x = 0; x < 6; x += 1)
{
    cout << "Please enter the High for day: "<< x+1<<": ";
    cin >> high;
    temperatures[0][x] = high;
}
for(int x = 0; x < 6; x += 1)
{
    cout << "Please enter the Low for day: "<< x+1<<": ";
    cin >> low;
    temperatures[1][x] = high;
}
//Error is here
calcAverage(high_average, low_average);
// end error   
system("pause");        
}


void calcAverage(double temperatures[6][1],double &high_average, double &low_average)
{
float accumulator = 0.0;
//for hot average  
for(int x = 0; x < 6; x += 1)
{
    accumulator += temperatures[0][x];
}
    high_average = accumulator;

// for cold average 
    accumulator = 0.0;
for(int x = 0; x < 6; x += 1)
{
    accumulator += temperatures[1][x];
}
    low_average = accumulator;
}

44 无法将double' todouble ()[2]' 转换为参数1' tovoid calcAverage(double ()[2])'

【问题讨论】:

  • 错误很明显,类型不匹配。您正在将 double 传递给期望 double()[2] 的函数

标签: c++ c++11 compiler-errors dev-c++


【解决方案1】:
void calcAverage(double temperatures[7][2]);

好的,calcAverage 接受一个二维数组。

calcAverage(high_average, low_average);

但是你通过了两次双打。

void calcAverage(double temperatures[6][1],double &high_average, double &low_average)

现在它需要一个二维数组和两个引用。

从这三个中选择一个并坚持下去。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-08
    • 2020-06-30
    • 2023-03-30
    • 2013-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多