本文介绍了C ++阵列 - 前presssion必须有一个恒定的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我得到一个错误,当我尝试从我声明的变量数组。
INT行= 8;
INT COL = 8;
INT [行] [COL];
为什么我得到这个错误:
解决方案
When creating an array like that, its size must be constant. If you want a dynamically sized array, you need to allocate memory for it on the heap and you'll also need to free it with delete when you're done:
//allocate the array
int** arr = new int*[row];
for(int i = 0; i < row; i++)
arr[i] = new int[col];
// use the array
//deallocate the array
for(int i = 0; i < row; i++)
delete[] arr[i];
delete[] arr;
If you want a fixed size, then they must be declared const:
const int row = 8;
const int col = 8;
int arr[row][col];
Also,
int [row][col];
doesn't even provide a variable name.
这篇关于C ++阵列 - 前presssion必须有一个恒定的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!