首页 > 解决方案 > 无法分配给 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
}

标签: c++

解决方案


正如错误消息所说,您不能分配给数组,但可以初始化它。

如果要将数组设置为特定值,请使用例如std::fill

std::fill(std::begin(deque), std::end(deque), 0);  // Set all elements of the array to zero

您也可以根据需要使用std::array分配的。


推荐阅读