c++ - 无法分配没有可行重载 '=' 错误的迭代器

标签 c++ stl unique-ptr

我有一个字段定义为

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/

相关文章:

c++ - 有没有比这更简洁的方法来初始化 unique_ptr<char[]> ?

c++ - 如何在 C++ Win32 中捕获鼠标在标题栏上的移动?

c++ - 无法将 std::chrono 与 std::future 一起使用 - 未找到 GLIBCXX_3.4.19

c++ - 当需要访问容器时返回迭代器而不是底层容器本身

c++ - 用于通过快速迭代按值从任何位置删除的容器

c++ - 以 cv::Point 为键的 std::map

c++ - unique_ptr 到派生类作为函数的参数,该函数将 unique_ptr 带到基类

C++ unique_ptr<Base> 指向 Derived 的 ptr

c++ - 从对象创建 xml

c++ - cout在哪里声明?