类型不匹配

Type Mismatches

本文关键字:不匹配 类型      更新时间:2023-10-16

我面临的错误是编译器说cannot convert ‘double*’ to ‘double’ in assignment。我的代码如下。

double* initArray(int data[], int dimensionCount){
    //data: element 0= number of dimensions
    //element 1 through inf define the size of each dimension
    //dimensionCount keeps track of the current dimension being manipulated by the function

    //Allocate dynamic memory for the current dimension
    double* output;
    output=new double[data[dimensionCount]];
    int i=dimensionCount;
    while(i<data[dimensionCount]){
        if( !(output[i]= initArray(data, dimensionCount++))){
            std::cout<< "Error! Out of Memory!";
            break;
        }
    i++;
    }
//returning the generated array tacks on the generated dimension to the lower dimension preceding it
return output;
}

由于outputdouble*类型并且arrayInit返回类型为double*的变量,我不知道它在哪里尝试从double转换为double*。我发现了这个,但它似乎不适用,因为data被正确传递,并且intArray返回指向正在生成的数组的指针,而不是数组本身,所以不应该有任何类型与output不匹配。

我更多地使用了这个函数,并意识到尽管output是一个double*并且与initArray返回的内容不冲突,但output[i]只是一个常规double,因为它是output所指向的。类型不匹配是由于尝试将output[i]设置为double*而导致的。

为了解决这个问题,我修改了函数以返回double而不是double*,然后确保output被取消引用到一个新的变量array。这允许函数返回一个普通数组,防止类型不匹配,并使函数的实际结果更可用。

这是代码:

double initArray(int data[], int dimensionCount){
//data: element 0= number of dimensions
//element 1 through inf define the size of each dimension
//dimensionCount keeps track of the current dimension being manipulated by the function

//Allocate dynamic memory for the current dimension
double* output;
output=new double[data[dimensionCount]];
int i=dimensionCount;
while(i<data[dimensionCount]){
    if( !(output[i]= initArray(data, dimensionCount++))){
        std::cout<< "Error! Out of Memory!";
        break;
    }
    i++;
}
//returning the generated array tacks on the generated dimension to the lower dimension preceding it
double array=*output;
return array;
}

return *output;

会做这个伎俩。使用其他变量的任何特定原因?