数组类型 'float [size]'不可分配
Array type 'float [size]' is not assignable
我有一个模板方法,我试图在其中将对象放入数组中。不过,我一直收到标题错误。下面是我的以下代码:
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 ...
}
相关文章:
- 我可以为unordered_map分配特定的模组值吗?
- 堆分配对于大型块中的分页是否更有效?
- 如何接受可修改和不可修改的参数?
- 可移动但不可复制的对象:按值传递还是按引用传递?
- 在可拆卸线程完成操作时取消分配内存
- 可视化C++将分配移动到未初始化的对象?
- 在线程中使用堆可分配 >100MB 的 RAM
- 哪种类型特征表明该类型是可分配的?(元组,对)
- C++概念相同且可分配
- 如何使具有包含唯一指针的成员变量的类可复制可分配
- 使用 std::string 参数和不可移动/可复制参数构建 std::map
- 使用不可复制(但可移动)键移动地图分配时出错
- GCC C++11 删除移动可分配类的副本分配会阻止 std::sort 编译
- 如何使具有常量属性的类可分配
- 创建可分配的最大字符数组
- 如何使我自己的容器索引和可分配
- 为Fortran可分配程序在C中分配内存
- 移动包含向量<unique_ptr的可分配类<T>>
- 移动clang和gcc中可分配的lambda
- 对左值和右值的可分配引用