编译错误 std::vector<std::shared_ptr<T>>迭代器和擦除方法
Compiling errors std::vector<std::shared_ptr<T>> iterator and erase methods
我正在尝试用C++和SFML制作一个游戏。但是,我对Missile
类有一些问题。我有一个std::vector<std::shared_ptr<Missile>>
,我想做的是在导弹超出射程时将其移除。
我对StackOverflow做了一些研究,发现了这样的东西:
auto list = ship->getMissilesToDisplay(); // returns the std::vector<std::shared_ptr<Missile>>
auto iterator = std::find(list.begin(), list.end(), [](const std::shared_ptr<Missile>& m) { return m->canDelete(); });
list.erase(iterator);
但当我编译这个时,我会遇到一些错误:
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includexutility(3579): error C2678: binary '==': no operator found which takes a left-hand operand of type 'std::shared_ptr<Missile>' (or there is no acceptable conversion)
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includesystem_error(284): note: could be 'bool std::operator ==(const std::error_condition &,const std::error_condition &) noexcept'
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includesystem_error(278): note: or 'bool std::operator ==(const std::error_condition &,const std::error_code &) noexcept'
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includesystem_error(272): note: or 'bool std::operator ==(const std::error_code &,const std::error_condition &) noexcept'
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includesystem_error(266): note: or 'bool std::operator ==(const std::error_code &,const std::error_code &) noexcept'
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includeexception(330): note: or 'bool std::operator ==(const std::exception_ptr &,std::nullptr_t) noexcept'
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includeexception(325): note: or 'bool std::operator ==(std::nullptr_t,const std::exception_ptr &) noexcept'
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includeexception(320): note: or 'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &) noexcept'
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includememory(1615): note: or 'bool std::operator ==<Missile>(const std::shared_ptr<Missile> &,std::nullptr_t) noexcept'
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includexutility(3579): note: or 'built-in C++ operator==(bool (__cdecl *)(const std::shared_ptr<Missile> &), bool (__cdecl *)(const std::shared_ptr<Missile> &))'
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includexutility(3579): note: or 'built-in C++ operator==(bool (__stdcall *)(const std::shared_ptr<Missile> &), bool (__stdcall *)(const std::shared_ptr<Missile> &))'
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includexutility(3579): note: or 'built-in C++ operator==(bool (__fastcall *)(const std::shared_ptr<Missile> &), bool (__fastcall *)(const std::shared_ptr<Missile> &))'
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includexutility(3579): note: or 'built-in C++ operator==(bool (__vectorcall *)(const std::shared_ptr<Missile> &), bool (__vectorcall *)(const std::shared_ptr<Missile> &))'
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includexutility(3579): note: while trying to match the argument list '(std::shared_ptr<Missile>, const Game::clean::<lambda_42809c80e7e2bc392c11a08cf362033b>)'
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includexutility(3592): note: see reference to function template instantiation '_InIt std::_Find_unchecked1<_InIt,_Ty>(_InIt,const _InIt,const _Ty &,std::false_type)' being compiled
1> with
1> [
1> _InIt=std::shared_ptr<Missile> *,
1> _Ty=Game::clean::<lambda_42809c80e7e2bc392c11a08cf362033b>
1> ]
1>c:program files (x86)microsoft visual studio2017enterprisevctoolsmsvc14.14.26428includexutility(3601): note: see reference to function template instantiation '_InIt std::_Find_unchecked<std::shared_ptr<Missile>*,_Ty>(const _InIt,const _InIt,const _Ty &)' being compiled
1> with
1> [
1> _InIt=std::shared_ptr<Missile> *,
1> _Ty=Game::clean::<lambda_42809c80e7e2bc392c11a08cf362033b>
1> ]
1>c:usersthàng longdocumentsvisual studio 2017projectsasteroidasteroidsrcgame.cpp(13): note: see reference to function template instantiation '_InIt std::find<std::_Vector_iterator<std::_Vector_val<std::_Simple_types<_Ty>>>,Game::clean::<lambda_42809c80e7e2bc392c11a08cf362033b>>(_InIt,const _InIt,const Game::clean::<lambda_42809c80e7e2bc392c11a08cf362033b> &)' being compiled
1> with
1> [
1> _InIt=std::_Vector_iterator<std::_Vector_val<std::_Simple_types<std::shared_ptr<Missile>>>>,
1> _Ty=std::shared_ptr<Missile>
1> ]
也许我误解了如何实现这段代码?
您使用一元谓词调用std::find
,但std::find
需要一个值类型来测试那里的相等性。因此,您尝试将std::shared_ptr<Missile>
与lambda进行比较,这当然是不可能的,并且会导致典型的可怕的模板错误消息。您可能想要使用std::find_if
,它接受一个谓词:
std::find_if(list.begin(), list.end(), [](const std::shared_ptr<Missile>& m) { return m->canDelete(); });
编辑:以上解决方案将允许您删除可以删除的第一个导弹。如果您想删除每个可以删除的导弹,可以使用std::remove_if
。它的用法带有一个令人困惑的习语,但我会尽力解释一下。std::remove_if
还接受两个迭代器和一个一元谓词,但它实际上并没有删除元素(因为要做到这一点,它需要知道只给定迭代器的容器,这是不可能的(。相反,如果元素应该被删除,它会将它们拖到容器的后面,并向这些元素中的第一个返回一个新的迭代器。这与std::vector::erase(iterator first, iterator last);
耦合以删除那些尾随元素。现在对于实际的代码,使用缩进来帮助分离移动部件:
list.erase(
std::remove_if(
list.begin(),
list.end(),
[](const std::shared_ptr<Missile>& m){ m->canDelete(); }
),
list.end()
);
相关文章:
- 使用std::multimap迭代器创建std::list
- C++中std::resize(n)和std::shrink_to_fit之间的区别
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 从持续时间构造std::chrono::system_clock::time_point
- std::具有相同基类的类的变体
- std::向量与传递值的动态数组
- 使用std::vector的OpenCL矩阵乘法
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- std::condition_variable::wait()如何评估给定的谓词
- 如何获取std::result_of函数的返回类型
- std::原子加载和存储都需要吗
- 将对象移动到std::shared_ptr
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 是std :: set&lt; std :: future&gt;不可能存在
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- 使用 std::vector<boost::shared_ptr<Base_Class>> 或 boost::p tr_vector 的性能注意事项是什么<Base>
- std::map<std::set, double> AND std:<long>map< std::p air<long, long>, double>
- 如何获取std::vector<DMatch>