c++ - 无法分配给 int[10] 类型的成员
问题描述
我有一些简单的代码,例如下面的示例。
如何this
在此代码中使用?为什么作业不编译?
class Deque {
private:
int deque[10];
// ...
public:
void setDeque();
// ...
};
void Deque::setDeque() {
this->deque = {0}; // ... error on this line ....
// 'int [10]' is not assignable
}
解决方案
正如错误消息所说,您不能分配给数组,但可以初始化它。
如果要将数组设置为特定值,请使用例如std::fill
:
std::fill(std::begin(deque), std::end(deque), 0); // Set all elements of the array to zero
您也可以根据需要使用可std::array
分配的。
推荐阅读
- c# - 如何让调试器在断点处打印控制台日志(SQL)
- networking - 我们可以在任何虚拟环境中模拟 Edgecore Datacenter 交换机吗
- css - Sass 是否会自动生成一个 css-style.css 文件并放在 Sass 文件夹中?
- solr - 在 solr 的一个集合中索引两个用户类型信息
- nativescript-angular - 带有角度的nativescript中的搜索过滤器和列表视图
- dask - dask: astype() 得到了一个意外的关键字参数“错误”
- android - null 不能转换为非 null 类型 kotlin.collections.List - kotlin
- laravel - 如何检查 laravel 计数结果是否为空
- javascript - VideoJS - 暂停未聚焦的视频元素的流媒体
- javascript - 如何在 HTML 中导出选择标记的选定值