我有一个叫做冠军的类包含一个向量对(字符串和双)。当将向量元素与另一个字符串进行比较时,我遇到了一个问题。
#pragma once
#include <string>
#include<vector>
using namespace std;
class Champion : public Game
{
protected:
vector<std::pair<string, double> > melee_champ = { { "yasuo",0 }, { "garen",0 }, { "jax",0 }, { "fiora",0 }, { "fizz",0 } };
vector<std::pair<string, double> > ranged_champ = { {"varus",0 }, {"ezreal",0}, {"lux",0}, {"zoe",0}, {"vayne",0} };
public:
Champion();
void write_champ_to_file(string f_name);
void delete_champ(string type, string name);
};这是我的课程,在实现中我有:
void Champion::delete_champ(string type, string name)
{
if (type == "melee champ")
{
for (pair<string, double>& x : melee_champ)
{
if (x.first == name)
{
auto itr = std::find(melee_champ.begin(), melee_champ.end(), name);
melee_champ.erase(itr);
Champion::write_champ_to_file("temp.txt");
remove("champ.txt");
rename("temp.txt", "champ.txt");
}
}
}
}问题在于比较(x.first ==名称)。
如何使==操作员过载?
这是我得到的错误:
Error C2676二进制‘=’=‘:’std::C2676‘不定义此运算符或转换为预定义运算符
可接受的类型
发布于 2020-05-15 14:14:46
实际错误不在x.first == name中,而是在调用std::find()中,因为name (即 std::string )将与每个std::pair< std::string、double>进行比较。
与其重载操作符==本身,您还可以通过传递这样一个lambda来使用==:
auto itr = std::find_if(melee_champ.begin(), melee_champ.end(),
[&name](pair<string, double> const& p) {
return p.first == name;
});https://stackoverflow.com/questions/61819685
复制相似问题