类型不匹配
Type Mismatches
我面临的错误是编译器说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;
}
由于output
是double*
类型并且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;
会做这个伎俩。使用其他变量的任何特定原因?
相关文章:
- 与'operator='不匹配(操作数类型'String'且"void")
- C++模板/别名 - 模板参数列表中参数 1 处的类型/值不匹配
- 错误:"模板<类_Tp,类_Dp>类 std::unique_ptr"的模板参数列表中参数 1 的类型/值不匹配
- 将 unordered_map 与 Catch2 谓词一起使用时类型不匹配
- 错误:'operator>'不匹配(操作数类型为 'Student' 和 'Student'
- 与"运算符>>"不匹配(操作数类型为"QDataStream"和"QJsonObject")
- 与'operator<<'不匹配(操作数类型'std::ostream {aka std::basic_ostream<char>}'
- 不同c++编译器之间的自动类型推导不匹配
- 重载函数的地址与所需类型不匹配
- 与"operator<<"不匹配(操作数类型'std::ostream'
- 参数错误可能与类型不匹配有关?
- 作为模板类型参数,为什么 type[N] 与其专用版本不匹配----模板<类 T>类 S<T[]>
- 模板参数列表中参数 3 处的类型/值不匹配
- C++ 重载:[错误] 与'operator='不匹配(操作数类型为 'String' 和"字符串")
- 调用子例程时类型不匹配
- 如果方法不进行类型检查,为什么C++模板匹配?
- 错误:与"运算符>>"不匹配(操作数类型为"std::istream
- 与"运算符[]"不匹配(操作数类型为"std::unique_ptr<std::vector<int> >"和"int")
- 错误:与'operator='不匹配(操作数类型为 'PlDrustvo' 和"PlDrustvo")|
- 警告 634:相等或有条件的强类型不匹配(类型"bool")