c++ 无法将参数 1 的“double”转换为“double*”到“void sort(double*,int)”错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30541367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ cannot convert 'double' to 'double*' for argument 1 to 'void sort(double*,int)' error
提问by Inkyu Lee
I'm a student listening to c programming lesson, and I'm using c++ to 'call-by-reference'. I don't know how to use c++ exactly, so I use c and save it into .cpp file. Anyway, I used a function to sort an array, and I now have an error. What should I do to solve this error?
我是一名学生,正在听 c 编程课,我正在使用 c++ 来“按引用调用”。我不知道如何准确地使用 c++,所以我使用 c 并将其保存到 .cpp 文件中。无论如何,我使用一个函数对数组进行排序,现在出现错误。我应该怎么做才能解决这个错误?
#include <stdio.h>
#include <math.h>
double round(double value);
void sort(double a[],int cnt);
void swap(double& x,double& y);
int main()
{
int i;
double array[3];
for(i=0;i<3;i++){
scanf("%lf",&array[i]);
}
sort(array[3],3);
printf("%d %d %d",ceil(array[0]),floor(array[2]),round(array[1]));
return 0;
}
double round(double value)
{
return floor(value+0.5);
}
void sort(double a[],int cnt)
{
int i,j;
for(i=0;i<cnt-1;i++){
for(j=i+1;j<cnt;j++){
if(a[i]<a[j]){
swap(a[i],a[j]);
}
}
}
}
void swap(double& x,double& y)
{
int imsi=x;
x=y;
y=imsi;
}
回答by vsoftco
Your sort(double a[], int cnt)
function takes as first argument double a[]
, which is syntactic sugar for a pointer double*
to the first element of an array. However in main()
you invoke it as
您的sort(double a[], int cnt)
函数将作为第一个参数double a[]
,它是指向double*
数组第一个元素的指针的语法糖。但是在main()
你调用它作为
sort(array[3], 3); // you pass a double here, not a double*
In the call above you pass the 4-th element of the array array
, i.e. a double
, not a pointer double*
. To pass a pointer to the first element of the array, replace the above call with
在上面的调用中,您传递了数组的第 4 个元素array
,即 a double
,而不是指针double*
。要传递指向数组第一个元素的指针,请将上述调用替换为
sort(array, 3); // you now pass a double* (i.e. pointer to first element of the array)
The compiler is basically tell you what's wrong:
编译器基本上是告诉你出了什么问题:
error: cannot convert 'double' to 'double*' for argument '1' to 'void sort(double*, int)' sort(array[3],3);
错误:无法将参数 '1' 的 'double' 转换为 'double*' 到 'void sort(double*, int)' sort(array[3],3);
It expects a double*
but you pass a double
. It attempts to convert double
to double*
, but such conversion is impossible, hence the error.
它期望 adouble*
但你通过了 a double
。它尝试转换double
为double*
,但这种转换是不可能的,因此出现错误。
回答by user700390
In addition to your main concern (already answered by another user), you should probably change your swap()
function so that you aren't using an int
to store a double
. This could result in unexpected behavior.
除了您的主要关注点(已经由另一个用户回答),您可能应该更改您的swap()
函数,以便您不使用 anint
来存储double
. 这可能会导致意外行为。
回答by Demelition
Next problem which you'll facing with:
您将面临的下一个问题:
printf("%d %d %d",ceil(array[0]),floor(array[2]),round(array[1]));
for print float you may use "%f"
.
对于打印浮点数,您可以使用"%f"
.
Simply to use cin
and cout
from library iostream
instead of scanf
and printf
简单地使用cin
和cout
从库iostream
而不是scanf
和printf
for(i=0;i<3;i++) {
std::cin>>array[i]>>' ';
}
for(i=0;i<3;i++) {
std::cout>>array[i]>>' ';
}
btw http://www.cplusplus.com/reference/could be useful for you on many reasons.
顺便说一句,http://www.cplusplus.com/reference/可能对您有用,原因有很多。