我有一个字段定义为
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();
应该也可以。
关于c++ - 无法分配没有可行重载 '=' 错误的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29066772/