无法分配没有可行的重载“=”错误的迭代器

Can't assign iterator with no viable overloaded '=' error

我有一个字段定义为

const vector<record>* data;

其中记录定义为

const unique_ptr<vector<float>> features;
const float label;

在我的主要代码中,我使用

vector<record>::iterator iter = data->begin()

编译器对我的代码不满意,因为在该迭代器赋值行出现 no viable overloaded '=' 错误。它还会产生此警告:

/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iterator:1097:7: note: candidate function (the implicit copy assignment operator) not viable: no known conversion from '__wrap_iter<const_pointer>' to 'const __wrap_iter<class MLx::Example *>' for 1st argument

我做错了什么?

"an iterator should be lightweight and should now own the data, i.e. there should be no attempt to copy or even touch record when I make the assignment."

它与迭代器无关 拥有 存储在数据 data 中的数据,但 const unique_ptr<> 限制访问模板参数类型仅作为 const 个实例。
这意味着您需要使用

vector<record>::const_iterator iter = data->begin();
             // ^^^^^^

在你的主代码中。

这很像写作

const vector<record>* data;

正如@Jonathan Potter在他的评论中提到的那样

auto iter = data->begin();

应该也可以。