为什么此代码显示“无法将'double'转换为'double *'”错误? [英] Why does this code show the "cannot convert 'double' to 'double*'" error?

查看:2812
本文介绍了为什么此代码显示“无法将'double'转换为'double *'”错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑此代码:

  double dist(double x [3],double y [3])
{
double Sum;
double distance;

for(int i = 0; i <3; i ++)
{
cout< 输入第一坐标值;
cin>> x [i];
cout<< 输入第二坐标值;
cin>> x [i];

Sum = Sum + pow((x [i] -x [i]),2.0);
distance =(int)sqrt(sum);
}
cout<< DISTANCE:<<距离;
return distance;
}

int main()
{
int i,n;
double x [3]; double y [3];
cout<< 输入测试用例的数量< endl;
cin>> n;
for(i = 0; i {
dist(x [i],y [i]);
}
return 0;
}

dist(x [i] y [i])它会一直给出错误


无法将'double'转换为'double *'参数'1'至'double dist(double *,double *)'



解决方案

不是如何传递数组到一个函数:

  for(i = 0; i  dist(x [i],y [i]); 
}

您可能认为您正在调用函数 dist ( y / code>)。这不是这样。因为你在一个循环中,所以调用该函数的次数与循环运行的次数一样多,在这种情况下, n 次。



此外,作为参数传递给函数的值是 double 类型的元素。这不是你认为你提供的数组。






我知道错误消息看起来很奇怪。 double * 在哪里?你可能问自己。答案很简单:


$ b

由于称为类型衰减,当数组在表达式中使用时,它们被转换为指向它们的第一个元素(基地址)的指针。 dist dist(double x [3],double y [3] )的参数类型完全等价于 dist(double * x,double * y)。这意味着你不正确地尝试传递 double 编译器报告的函数不能将它们转换为指针。



传递数组的名称将传递一个指针到第一个元素,这将解决你的主问题:

  dist(x,y); 

您仍然可以在指针上使用下标运算符 x [n] 的原因是因为下标运算符是语法糖 *(x + n)。也就是说,一个指针由 n 加上一个取消引用,该地址。






此类型衰减还带来另一个问题:由于数组被转换为指针,你可以有一个函数,它接受一个 double [100] 并传递一个数组包含1元素,因为当数组衰减到指针,它们的类型完全匹配。有以下几种方法:




  • 将参数作为函数的引用/指针

  • 使用标准库数组类



现在,

  double dist(double(& x) [3],双(& y)[3]); 

类型衰减不会发生在这里,你有一个有保证的大小,因为编译器会报告错误if已传入任何其他大小的数组。



引用非常有用,但可以关闭第一个计时器。幸运的是,标准库为我们提供了一个数组类 std :: array 。不需要奇怪的语法,它有一个更干净的接口 - 没有指针!

  #include< array& 
double dist(std :: array< double,3> x,std :: array< double,3> y);

std :: array< double,3> x,y;
dist(x,y);


Consider this code:

double dist(double x[3], double y[3])
{
    double Sum;
    double distance;

    for(int i=0; i<3; i++)
    {
        cout << "Enter value of first coordinate";
        cin >> x[i];
        cout << "Enter value of second coordinate";
        cin >> x[i];

        Sum = Sum + pow((x[i] - x[i]), 2.0);
        distance = (int)sqrt(Sum);
    }
        cout << "DISTANCE: " << distance;
    return distance;
}

int main()
{
    int i, n;
    double x[3]; double y[3];
    cout << "enter number of test case" << endl;
    cin >> n;
    for(i=0; i<n; i++)
    {
        dist(x[i], y[i]);
    }
    return 0;
}

On the line dist(x[i], y[i]) it keeps giving the error

cannot convert 'double' to 'double*' for argument '1' to 'double dist(double*, double*)'

解决方案

This is not how you pass an array to a function:

for (i = 0; i < n; i++) {
    dist(x[i], y[i]);
}

You probably thought that you were calling the function dist once with an array containing all the values from x[0..n-1] (and likewise for y). This is not so. Since you are in a loop, you are calling the function for as many times as the loop runs, which in this case is n times.

Furthermore, the values you are passing as parameters to the function are elements of type double. It is not the array that you thought you were providing.


And I know that the error message seems strange. "Where is double* coming from?" you may have asked yourself. Well the answer is simple:

Arrays cannot be passed to function.

Because of something called "type decay", arrays are converted into pointers to their first element (the base address) when they are used in expressions. The parameter types for dist (dist(double x[3], double y[3]) are exactly equivalent to dist(double* x, double* y). This means while you were incorrectly trying to pass double's to the function the compiler reported that you couldn't convert them to pointers.

Passing the name of the array will pass a pointer to the first element. This will solve your main issue:

dist(x, y);

The reason you can still use the subscript operator x[n] on a pointer is because the subscript operator is syntactical sugar for *(x + n). That is, a pointer offset by n plus a dereference to give you the value at that address.


This type decay also entails another problem: since the array is being converted into a pointer, the size information that comes along with its type can't be determined. You can have a function that takes a double[100] and pass it an array that holds 1 element because when the array decays to a pointer their types match exactly. There are a few ways around this:

  • Make the parameter a reference/pointer to a function
  • Use a standard library array class

Now, a pointer and a reference are two different things, but they can still be used to solve this problem. Using a reference to an array is different than actually passing in an array itself. References act as aliases to objects, and they are basically a pointer under the hood with a few exceptions (see the provided link). A reference is good enough for our example however, here is what it looks like:

double dist(double (&x)[3], double (&y)[3]);

Type decay will not occur here, and you have a guaranteed size because the compiler will report an error if an array of any other size was passed in.

A reference is useful, but it can be off putting for first timers. Fortunately the standard library provides us with an array class std::array. No strange syntax is needed and it has a cleaner interface - no pointers!

#include <array>
double dist(std::array<double, 3> x, std::array<double, 3> y);

std::array<double, 3> x, y;
dist(x, y);

这篇关于为什么此代码显示“无法将'double'转换为'double *'”错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆