c++ - C ++从向量中删除对象?
问题描述
我想Attack
从播放器类中名为“movelist”的向量中删除一个类对象。我尝试使用它并收到此错误:
error: no match for 'operator==' (operand types are 'Attack' and 'const Attack')
有什么建议么?
void Player::del_attack(Attack a1)
{
Attack to_rmv=a1;
movelist.erase(std::remove(movelist.begin(), movelist.end(), to_rmv), movelist.end());
}
我有
#include <algorithm>
#include <vector>
解决方案
问题是你还没有operator==
为你的Attack
班级定义。如果您考虑一下,这是必要的。要从向量中删除等于 的元素to_rmv
,算法需要知道如何测试两个Attack
对象是否相等。
最简单的答案是定义operator==
for Attack
,例如:
// return true if x equals y, false otherwise
bool operator==(const Attack& x, const Attack& y)
{
// your code goes here
}
您可能需要将此运算符设为friend
您的Attack
班级。
推荐阅读
- python - 我想为音频文件提取 MFCC 特征
- wordpress - 使用 Wordpress 安装子域重定向到根域
- flutter - Flutter 运行旧应用程序但不运行新应用程序
- android - 反应原生 - 无法从共享意图中获取 url
- google-cloud-firestore - 最佳实践:编辑帖子标题同时也是页面上数据的文档 ID?
- heroku - 尝试在 Heroku 上部署胖 Jar 时出错
- spring-jdbc - 如何在 JdbcTemplate 中自动将行映射到类?
- r - 无法以闪亮的方式显示绘图热图
- ruby-on-rails - jwt_session rails 6 令牌过期
- c - 使用select的客户端和服务器通信不能用C相互发送和接收