我在传递非const二维指针数组作为函数的const参数时遇到问题。但是我遇到一个错误。我不明白为什么。
// Online C++ compiler to run C++ program online
#include <iostream>
void test(const int *arrayPtr[][10]){}
//void test(int * const arrayPtr[][10]){//Works DO NOT USE //}
int main() {
int *arrayPtr[10][10] = {};
test(arrayPtr);
std::cout <<"done" << std::endl;
return 0;
}
g++ /tmp/JYRlXRFoja.cpp /tmp/JYRlXRFoja.cpp: In function 'int main()': /tmp/JYRlXRFoja.cpp:11:7: error: cannot convert 'int* (*)[10]' to 'const int* (*)[10]' 11 | test(arrayPtr);
| ^~~~~~~~
| |
| int* (*)[10] /tmp/JYRlXRFoja.cpp:5:42: note: initializing argument 1 of 'void test(const int* (*)[10])'
5 | void test(const int *arrayPtr[][10]){
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
您的代码中有两个错误。
int* array[][]
,而不是const int* array[][]
。因此将功能参数更改为int* array[][10]
。int* (&arrayPtr)[][10]
。您的错误是不言自明的,它说出了什么问题。如果仔细阅读,它表示您将传递int* (*)[10]
到const int* (*)[10]
类型容器。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句