错误:对于数组下标,无效类型“double*[double]”。

时间:2021-06-08 16:26:18

I'm trying to create a programme which will place all of the real numbers in an array - with the ordering of the numbers taking place in a separate function - into descending order, and print them out.

我正在尝试创建一个程序,它将把所有实数都放在一个数组中,并将它们的顺序排列成一个单独的函数,然后将它们打印出来。

The following is the programme as I have it so far, but there are 2 issues with it, according to the compiler: (i) On line 22 ("return N[t];"), I get "error: invalid types 'double*[double]' for array subscript".
(ii) On line 28 ("cout << sort_array(Q[100]) << " " "), I get "error: cannot convert 'double' to 'double*' for argument '1' to 'double* sort_array(double*)'".

下面是我目前为止的程序,但是有两个问题,根据编译器:(I)在第22行(“返回N[t];”),我得到了“错误:无效类型的双*[double]”的数组下标。(ii)第28行("cout << sort_array(Q[100]) << "),我得到了“错误:不能将‘double’转换为‘double*’,用于参数‘1’到‘double* sort_array(双*)’”。

I'm not quite understanding why these two errors are coming up, but I would love some help in resolving them.

我不太明白这两个错误为什么会出现,但我希望能帮助解决它们。

#include <iostream>
#include <cstdlib>

using namespace std;

double *sort_array (double *N) {
double t;
    int size=100, a, b;

for (t=0; t<size; t++)
        *N = rand()%250;

    for (a=1; a<size; a++) {
        for (b=size-1; b>=a; b--) {
            if (N[b-1] < N[b]) {
                t = N[b-1];
                N[b-1] = N[b];
                N[b] = t;
            }
        }
    }
    return N[t];
}

int main()
{
    double Q[100];
        cout << sort_array(Q[100]) << " ";
        cout << endl;


    return 0;
}

2 个解决方案

#1


0  

The problem is the sort_array(Q[100]) statement. This is telling the compiler to call sort_array with the 101st double in the Q array (which is actually out of bounds). You really wanted to just pass in Q instead of Q[100].

问题是sort_array(Q[100])语句。这告诉编译器在Q数组中调用sort_array(实际上是超出边界的)。你真的想要通过Q而不是Q[100]。

However, note that passing C-style arrays as double* for example loses the length information and is not canonical C++. Instead you can use vector to carry the array and size with it.

但是,请注意,通过C样式的数组作为double*会丢失长度信息,而不是规范的c++。相反,您可以使用vector来携带数组和大小。

EDIT: And since you're modifying the data in-place there is no need for your function to return anything at all. Change it to void and just skip the return at the end. You'll then have to iterate over the vector/array to print out each of the elements. cout doesn't provide builtin capability to print aggregates.

编辑:既然你正在修改数据,你的函数就不需要返回任何东西。将其更改为void,并在结束时跳过返回。然后,您必须遍历vector/array以打印出每个元素。cout不提供打印聚合的内置功能。

Finally a book from The Definitive C++ Book Guide and List might help get you up to speed on C++ concepts.

最后,一本权威的c++书籍指南和清单可能会帮助你加快c++的概念。

#2


0  

The first error is because N[t] is a double (it means "The t'th element of N"), but your function returns a double*. Your function doesn't look like it should return anything, actually. It sorts the data pointed to by N, so there's no need to return anything. You should probably switch to a void return value.

第一个错误是因为N[t]是一个double(它的意思是“N”的t元素),但是你的函数返回一个double*。实际上,函数看起来不应该返回任何东西。它对N的数据进行排序,所以不需要返回任何东西。您可能应该切换到void返回值。

The second error is because Q[100] is a double (it means "the 101th element of Q, which is an error anyway since the last element of Q is Q[99], as array indexes in C++ begin at 0, not 1), but your function expects a double. I assume that what you actually mean to do is:

第二个错误是因为Q[100]是一个double(它意味着“Q的第101个元素,这是一个错误,因为Q的最后一个元素是Q[99],因为c++中的数组索引从0开始,而不是1),但是您的函数需要一个double。我认为你真正想做的是:

sort_array(Q)

to pass the pointer to the first element directly.

将指针直接传递给第一个元素。

Remember that when passing arrays around, you only need to pass the address of the arrays's first element. In this case, Q, which is equivalent to &Q[0] but is easier to write.

请记住,当传递数组时,只需要传递数组的第一个元素的地址。在这种情况下,Q等于和Q[0],但更容易写。

#1


0  

The problem is the sort_array(Q[100]) statement. This is telling the compiler to call sort_array with the 101st double in the Q array (which is actually out of bounds). You really wanted to just pass in Q instead of Q[100].

问题是sort_array(Q[100])语句。这告诉编译器在Q数组中调用sort_array(实际上是超出边界的)。你真的想要通过Q而不是Q[100]。

However, note that passing C-style arrays as double* for example loses the length information and is not canonical C++. Instead you can use vector to carry the array and size with it.

但是,请注意,通过C样式的数组作为double*会丢失长度信息,而不是规范的c++。相反,您可以使用vector来携带数组和大小。

EDIT: And since you're modifying the data in-place there is no need for your function to return anything at all. Change it to void and just skip the return at the end. You'll then have to iterate over the vector/array to print out each of the elements. cout doesn't provide builtin capability to print aggregates.

编辑:既然你正在修改数据,你的函数就不需要返回任何东西。将其更改为void,并在结束时跳过返回。然后,您必须遍历vector/array以打印出每个元素。cout不提供打印聚合的内置功能。

Finally a book from The Definitive C++ Book Guide and List might help get you up to speed on C++ concepts.

最后,一本权威的c++书籍指南和清单可能会帮助你加快c++的概念。

#2


0  

The first error is because N[t] is a double (it means "The t'th element of N"), but your function returns a double*. Your function doesn't look like it should return anything, actually. It sorts the data pointed to by N, so there's no need to return anything. You should probably switch to a void return value.

第一个错误是因为N[t]是一个double(它的意思是“N”的t元素),但是你的函数返回一个double*。实际上,函数看起来不应该返回任何东西。它对N的数据进行排序,所以不需要返回任何东西。您可能应该切换到void返回值。

The second error is because Q[100] is a double (it means "the 101th element of Q, which is an error anyway since the last element of Q is Q[99], as array indexes in C++ begin at 0, not 1), but your function expects a double. I assume that what you actually mean to do is:

第二个错误是因为Q[100]是一个double(它意味着“Q的第101个元素,这是一个错误,因为Q的最后一个元素是Q[99],因为c++中的数组索引从0开始,而不是1),但是您的函数需要一个double。我认为你真正想做的是:

sort_array(Q)

to pass the pointer to the first element directly.

将指针直接传递给第一个元素。

Remember that when passing arrays around, you only need to pass the address of the arrays's first element. In this case, Q, which is equivalent to &Q[0] but is easier to write.

请记住,当传递数组时,只需要传递数组的第一个元素的地址。在这种情况下,Q等于和Q[0],但更容易写。