2D と 1D の 2 つの配列を宣言しようとしています。次元が const 値である必要があることはわかっています。そのため、const 値は関数呼び出しの戻り値から割り当てられます。それはうまくいきますが、派生した値を使用して配列を宣言すると、COMPILE エラーが発生します。どうして???
これが私のコードです:
int populateMatrixFromFile(string fname) {
std::ifstream fileIn;
int s = determineDimensions(fname); // return value (CONST INT)
const int size = s; // assign to const
cout << "Value returned from determineDimensions(): " << size << endl;
if (size > 10){
cout << "Maximum dimensions for array is 10 rows and 10 columns. Exiting" << endl;
return 1;
}
fileIn.open(fname.c_str(), ios::in); //opened for reading only.
float aMatrix[size][size]; // ERROR
float bMatrix[size]; // ERROR
しかし、それはここで動作します:
// assign the pth row of aMatrix to temp
const int alen = sizeof (aMatrix[p]) / sizeof (float);
float temp[alen]; // WORKS!!!
for (size_t i = 0; i < alen; i++) {
temp[i] = aMatrix[p][i];
}
助けてくれてありがとう。