【问题标题】:argument of type double is incompatible with parameter type double pointerdouble 类型的参数与参数类型双指针不兼容
【发布时间】:2013-11-22 02:44:00
【问题描述】:

试图找到最低分和最高分,但我不断收到错误

argument of type "double" is incompatible with parameter of type "double*"

代码:

cout << "The lowest of the results = " << find_lowest(score[5]);
cout << "The highest of the results = " << find_highest(score[5]);

system("Pause");


}

double find_highest(double a[])
{
double temp = 0;
for(int i=0;i<5;i++)
{
    if(a[i]>temp)
        temp=a[i];
}
return temp;
}

double find_lowest(double a[])
{
double temp = 100;
for(int i=0;i<5;i++)
{
    if(a[i]<temp)
        temp=a[i];
}
return temp;
}

【问题讨论】:

  • 您的函数需要一个 double 数组,但您只传递了一个 double 元素。 score[5] 表示“名为 score 的数组的第 5 个元素”。
  • “感谢支持”作为提问主题绝对没有意义,对于本站以后的用户在搜索时使用毫无价值。请edit 提供有用且与您的问题内容相关的内容。

标签: c++ types double


【解决方案1】:

正如@jogojapan 指出的那样,您将不得不更改这些

  cout << "The lowest of the results = " << find_lowest(score[5]);
  cout << "The highest of the results = " << find_highest(score[5]);

  cout << "The lowest of the results = " << find_lowest(score);
  cout << "The highest of the results = " << find_highest(score);

您的函数需要一个双精度数组,而不是双精度元素。

【讨论】:

    【解决方案2】:

    “double*”是指向“double”类型变量的指针。你写了

    cout << "The lowest of the results = " << find_lowest(score[5]);
    

    并且“score[5]”返回数组score 中的第6 个元素。你只需要

    cout << "The lowest of the results = " << find_lowest(score);
    

    在 C 和 C++ 中,数组可以衰减为指针,并在用作函数参数时这样做。 find_lowest score 期望 double a[] 衰减到 double*,但您试图将分数的第 6 个元素传递给它,即 double,而不是 double*

    【讨论】:

      【解决方案3】:

      问题在于表达式 find_lowest(score[5]), score[5] 是 double 类型,而在函数的参数列表中您指定了 double[]double* 因此出现错误。

      所以做以下更正:

      cout << "The lowest of the results = " << find_lowest(score);
      cout << "The highest of the results = " << find_highest(score);
      

      此外,您的 find_highest() 函数中存在一些错误,如果输入数组包含所有负数,例如 a[]={-1.0,-2.0,-3.5,-5.0,-1.3} 那么您的函数将return 0 不正确,正确的实现是:

      double find_highest(double a[])
      {
       double temp = a[0];
       for(int i=0;i<5;i++)
       {
        if(a[i]>temp)
           temp=a[i];
       }
       return temp;
      }
      

      find_lowest() 函数也应该是:

      double find_lowest(double a[])
      {
       double temp = a[0];
       for(int i=0;i<5;i++)
       {
        if(a[i]<temp)
           temp=a[i];
       }
       return temp;
      }
      

      【讨论】:

        【解决方案4】:

        score[5] 是分数数组 (0,1,2,3,4,5) 中位置 5 的双精度数。 如果你想发送分数数组的前 6 个元素,你可能想尝试这样的事情:

        find_lowest((int[]){score[0],score[1],score[2],score[3],score[4],score[5]});
        

        做这样的事情来使你的函数更灵活一点可能是一个更好的主意,但你需要保持正在使用的数组的长度(但我想你已经通过确保这样做了它们是 6 个或更多元素)。

        double find_highest(double a[], size_t len)
        {
           double temp = 0;
           for(size_t i=0;i<len;i++)
           {
               if(a[i]>temp)
               temp=a[i];
           }
           return temp;
        }
        

        【讨论】:

          猜你喜欢
          • 2019-07-11
          • 2021-03-13
          • 1970-01-01
          • 2018-09-07
          • 2021-07-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-10-11
          相关资源
          最近更新 更多