下面的独立程序部分导致 Visual Studio 2019 出现此错误:
"function "partition_data::operator=(const partition_data &)" (隐式声明)" 是一个被删除的函数。
基于对此类问题的研究,我定义了一个移动赋值运算符,因为 unique_ptr 但我仍然收到上述错误。在此示例中如何解决此错误?
#include <cstddef>
#include <memory>
#include <utility>
#include <vector>
struct partition_data
{
public:
partition_data(std::size_t size, double initial_value)
: data_(new double[size])
, size_(size)
{
for (std::size_t i = 0; i < size; ++i)
data_[i] = initial_value;
}
partition_data& operator=(partition_data&& other){
data_ = (std::move(other.data_));
size_ = other.size_;
}
partition_data(partition_data&& other) noexcept
: data_(std::move(other.data_))
, size_(other.size_)
{
}
private:
std::unique_ptr<double[]> data_;
std::size_t size_;
};
int main()
{
partition_data A = partition_data(5, 0.1);
partition_data B = partition_data(5, 0.2);
std::vector<partition_data> V(2);
V[0] = A; //here occurs the error
V[1] = B; //here occurs the error
}