C++ 无法将参数 1 从 'int**' 转换为 'const int **'
C++ cannot convert argument 1 from 'int**' to 'const int **'
我想以只读模式将 2D pointer/array 传递给函数。我在 1D 情况下尝试了它,它按我想要的方式工作,但对于 2D 阵列它没有!
错误信息是
cannot convert argument 1 from 'int**' to 'const int **'
我的代码在这里
void test_constdpointer(int const** pda){
cout << "function: \n";
for (int i = 0; i < 3; i++) {
cout << pda[i][0] << " " << pda[i][1] << " " << pda[i][2] << "\n";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int **da;
da = new int*[3];
for (int i = 0; i < 3; i++) {
da[i] = new int[3];
}
for (int i = 0; i < 3; i++) {
cout << da[i][0] << " " << da[i][1] << " " << da[i][2] << "\n";
}
test_constdpointer(da);
return 0;
}
您需要在 const
之前获取两个指针级别。
void test_constdpointer(int const* const* pda)
我想以只读模式将 2D pointer/array 传递给函数。我在 1D 情况下尝试了它,它按我想要的方式工作,但对于 2D 阵列它没有!
错误信息是
cannot convert argument 1 from 'int**' to 'const int **'
我的代码在这里
void test_constdpointer(int const** pda){
cout << "function: \n";
for (int i = 0; i < 3; i++) {
cout << pda[i][0] << " " << pda[i][1] << " " << pda[i][2] << "\n";
}
}
int _tmain(int argc, _TCHAR* argv[])
{
int **da;
da = new int*[3];
for (int i = 0; i < 3; i++) {
da[i] = new int[3];
}
for (int i = 0; i < 3; i++) {
cout << da[i][0] << " " << da[i][1] << " " << da[i][2] << "\n";
}
test_constdpointer(da);
return 0;
}
您需要在 const
之前获取两个指针级别。
void test_constdpointer(int const* const* pda)