gpt4 book ai didi

c++ - 自定义对象容器的迭代器

转载 作者:搜寻专家 更新时间:2023-10-31 00:21:18 27 4
gpt4 key购买 nike

如果我构建了一个我想包含在其中的类,例如一个集合,我将如何遍历该集合?我能说吗

 std::set<customObject>::iterator it

我以为我可以做到,但我遇到了以下一系列错误......

drawing.h:110: error: no match for ‘operator=’ in ‘it = ((object*)this)->object::objects. std::vector<_Tp, _Alloc>::begin [with _Tp = object, _Alloc = std::allocator<object>]()’
/usr/include/c++/4.2.1/bits/stl_tree.h:225: note: candidates are: std::_Rb_tree_const_iterator<object>& std::_Rb_tree_const_iterator<object>::operator=(const std::_Rb_tree_const_iterator<object>&)
drawing.h:110: error: no match for ‘operator!=’ in ‘it != ((object*)this)->object::objects. std::vector<_Tp, _Alloc>::end [with _Tp = object, _Alloc = std::allocator<object>]()’
/usr/include/c++/4.2.1/bits/stl_tree.h:292: note: candidates are: bool std::_Rb_tree_const_iterator<_Tp>::operator!=(const std::_Rb_tree_const_iterator<_Tp>&) const [with _Tp = object]
drawing.h:111: error: ‘struct std::_Rb_tree_const_iterator<object>’ has no member named ‘sketch’

这是我的代码:

void draw_in_place()
{
place();
std::set<object>::const_iterator it;
for(it = objects.begin(); it != objects.end(); it++){
*it.draw_in_place();
}
}

最佳答案

((object*)this)->object::objects. std::vector<_Tp, _Alloc>::begin

objects显然是 std::vector<object> , 不是 std::set<object> .因此,您需要使用 std::vector<object>::const_iterator .

*it.draw_in_place();

这是不正确的:您需要先取消引用迭代器以访问元素,然后再使用元素:

(*it).draw_in_place();
// or
it->draw_in_place();

关于c++ - 自定义对象容器的迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4801524/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com