首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >编译错误std::vector<std::shared_ptr<T>>迭代器和erase方法

编译错误std::vector<std::shared_ptr<T>>迭代器和erase方法
EN

Stack Overflow用户
提问于 2018-07-12 06:54:35
回答 1查看 148关注 0票数 0

我正在尝试用C++和SFML做一个游戏。但是,我对Missile类有一些问题。我有一个std::vector<std::shared_ptr<Missile>>,我想做的是在导弹超出射程时将其移除。

我在StackOverflow上做了一些研究,我发现了类似这样的东西:

代码语言:javascript
运行
复制
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);

但是当我编译这段代码时,我得到了一些错误:

代码语言:javascript
运行
复制
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\xutility(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 studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\system_error(284): note: could be 'bool std::operator ==(const std::error_condition &,const std::error_condition &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\system_error(278): note: or       'bool std::operator ==(const std::error_condition &,const std::error_code &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\system_error(272): note: or       'bool std::operator ==(const std::error_code &,const std::error_condition &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\system_error(266): note: or       'bool std::operator ==(const std::error_code &,const std::error_code &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\exception(330): note: or       'bool std::operator ==(const std::exception_ptr &,std::nullptr_t) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\exception(325): note: or       'bool std::operator ==(std::nullptr_t,const std::exception_ptr &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\exception(320): note: or       'bool std::operator ==(const std::exception_ptr &,const std::exception_ptr &) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\memory(1615): note: or       'bool std::operator ==<Missile>(const std::shared_ptr<Missile> &,std::nullptr_t) noexcept'
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\xutility(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 studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\xutility(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 studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\xutility(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 studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\xutility(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 studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\xutility(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 studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\xutility(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 studio\2017\enterprise\vc\tools\msvc\14.14.26428\include\xutility(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:\users\thàng long\documents\visual studio 2017\projects\asteroid\asteroid\src\game.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>        ]

也许我误解了如何实现这段代码?

EN

回答 1

Stack Overflow用户

发布于 2018-07-12 07:04:42

您正在使用一元谓词调用std::find,但是std::find需要一个值类型来测试那里的相等性。因此,您正在尝试将std::shared_ptr<Missile>与lambda进行比较,当然,这是不可能的,并导致通常可怕的模板错误消息。您可能希望使用std::find_if,它接受一个谓词:

代码语言:javascript
运行
复制
std::find_if(list.begin(), list.end(), [](const std::shared_ptr<Missile>& m) { return m->canDelete(); });

编辑:上面的解决方案可以让你移除第一枚可以删除的导弹。如果你想删除所有可以删除的导弹,你可以使用std::remove_if。它的用法有一个令人困惑的习语,但我会试着解释一下。std::remove_if还接受两个迭代器和一个一元谓词,但它实际上并不删除元素(因为要做到这一点,它需要知道只给出迭代器的容器,即impossible)。相反,如果应该删除元素,它会将元素洗牌到容器的后面,并将新的迭代器返回到这些元素的第一个。这与std::vector::erase(iterator first, iterator last);结合使用,可以删除那些尾随元素。现在来看实际的代码,使用缩进来帮助分隔移动部分:

代码语言:javascript
运行
复制
list.erase(
    std::remove_if(
        list.begin(),
        list.end(),
        [](const std::shared_ptr<Missile>& m){ m->canDelete(); }
    ),
    list.end()
);
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51295222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档