我写了这样的代码:
void Print(const int & dataArray[], const int & arraySize) { // problem
for(int i = 0; i<arraySize; i++) {
cout << dataArray[i] << " ";
}
cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
在mian()函数中:
`
int iArray[14] = { 7, 3, 32, 2, 55, 34, 6, 13, 29, 22, 11, 9, 1, 5 };
int numArrays = 14;
Print(iArray, numArrays);
....
`
Run Code Online (Sandbox Code Playgroud)
编译器说引用数组是非法的,为什么它是非法的?我看到<effective c ++>,它说建议我们使用const和reference,我只是尝试实现它(我是初学者),我也想知道void Print(const int dataArray[], const int & arraySize)参数我用const,&来限定arraySize,是不是?(或者它比int arraySize或const int arraySize好多了?),我也想使用const,&to dataArray [],但是我失败了.
c++ ×1