数组类型 'float [size]'不可分配

Array type 'float [size]' is not assignable

本文关键字:不可分 可分配 size float 类型 数组      更新时间:2023-10-16

我有一个模板方法,我试图在其中将对象放入数组中。不过,我一直收到标题错误。下面是我的以下代码:

template <class Object>
void SparseMat<Object>::showSubSquare(int start, int size) {
   for (int rowIndex = start; rowIndex < start + size; rowIndex++) {
      Object objectsInRow[size];
      FHlist<MatNode<Object>> wantedRow = matrix[rowIndex];
      for (iterator<MatNode<Object>> iter = wantedRow.begin(); iter != wantedRow.end(); ++iter) {
          MatNode<Object> m = *iter;
          objectsInRow[m.getCol()] = m.data; //ERROR
      }
      for (int colIndex = start; colIndex < start + size; rowIndex++) {
         if (objectsInRow[colIndex] == NULL) {
           cout << defaultValue << " ";
         }
         else {
           cout << objectsInRow[colIndex] << " ";
         }
      }
      cout << endl;
   }
}

作为参考,m.getCol(( 只返回一个 int,因此不会导致错误。此外,矩阵是列表的向量,FHlist 是具有相同基本效用的自实现列表。

我对模板函数的用法如下:

typedef SparseMat<float> SpMat;
int main()
{
   SpMat mat(MAT_SIZE, MAT_SIZE, 0); 
   mat.showSubSquare(0, 15);
}

确切的错误是"错误 C3863:数组类型'float [size]'不可分配">

'size' 对于编译时不是常量,这将导致崩溃;

计划A:

用 std::vector 更改它,

计划B:

使用"新类型[大小]">

计划C:

template <int _size> // this is the const value for the compiling time
void func()
{
   type array[_size];
   // your code ...
}