錯誤消息的功能:錯誤呼叫使用指針作爲參數
main.cpp: In function 'int main()':
main.cpp:39:52: error: expression list treated as compound expression in initializer [-fpermissive]
int* concatArray (*pArray1, size1, *pArray2, size2);
^
main.cpp:39:52: error: invalid conversion from 'int' to 'int*' [-fpermissive]
第39行的我的代碼,其中i調用concat函數;我從來沒有見過這個錯誤,我不知道如何解決它。
#include <iostream>
#include <cstdlib>
using namespace std;
int* concatArray (int* pArray1, int size1, int* pArray2, int size2);
int main(){
int size1 = 8;
int Array1 [size1];
for (int i = 0; i < size1; i++){
Array1[i] = rand() % 10;
}
int size2 = 10;
int Array2 [size2];
for (int i = 0; i < size2; i++){
Array2[i] = rand() % 10;
}
int* pArray1;
int* pArray2;
pArray1 = Array1;
pArray2 = Array2;
cout << "The first array contains: " << endl;
for (int i = 0; i < size1; i++){
cout << Array1[i] << endl;
}
cout << "The second array contains: " << endl;
for (int i = 0; i < size2; i++){
cout << Array2[i] << endl;
}
int* concatArray (*pArray1, size1, *pArray2, size2);
cout << "the concat array contains: " << endl;
for (int i = 0; i < size1 + size2; i++) {
cout << (concatArray + i) << endl;
}
return 0;
}
int* concatArray (int* pArray1, int size1, int* pArray2, int size2){
int* concatArray = new int [size1 + size2];
for (int i = 0; i < size1 + size2; i++) {
if(i < size1){
*(concatArray + i) = *(pArray1 + i);
}
else{
*(concatArray + i) = *(pArray2 + i);
}
}
return concatArray;
}
請避免指針和返回分配的內存 –