JavaEar 专注于收集分享传播有价值的技术资料

error: invalid types 'double*[double]' for array subscript

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'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. 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.

    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:

    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.

  2. 参考答案2
  3. 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].

    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.

    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.

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