double类型的参数与参数类型双指针不兼容

时间:2013-11-10 06:41:49

标签: c++ types double

试图找到最低分和最高分,我一直收到错误

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;
}

4 个答案:

答案 0 :(得分: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);

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

答案 1 :(得分:0)

“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得分期望double a[]会衰减至double*,但您尝试将其传递给得分的第6个元素double,而不是double*

答案 2 :(得分:0)

得分[5]是得分数组中位置5的双倍(0,1,2,3,4,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;
}

答案 3 :(得分:0)

问题是表达式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()函数中有一些错误,如果输入数组的所有负数都像[] = { - 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;
}