0

I have class called Champion contain a vector of pairs (string and double). I have a problem while comparing element of vector with another string.

#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);
};

This is my class and in the implementation I have :

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");
            }
        }
    }
}

The problem is in comparing (x.first == name).
How can I overload == operator ?

This is the error that I get:

Error C2676 binary '==': 'std::pair' does not define this operator or a conversion to a type acceptable to the predefined operator

Kashinath Patekar
  • 1,083
  • 9
  • 23
  • 3
    Don't remove things from container that you are iterating over. This will screw you over. What is the error message that you get? – Yksisarvinen May 15 '20 at 12:55
  • Error C2676 binary '==': 'std::pair' does not define this operator or a conversion to a type acceptable to the predefined operator – Yassine Mrabet May 15 '20 at 12:56
  • 4
    Your problem isn't the `if (x.first == name)` line, it's the `auto itr = std::find(melee_champ.begin(), melee_champ.end(), name);` line. You want to use `std::find_if` and pass in a function (a lambda would be nice and compact and pretty in your code) here's a question on the subject https://stackoverflow.com/questions/12008059/find-if-and-stdpair-but-just-one-element – JohnFilleau May 15 '20 at 13:00
  • Didn't help because i need to return the position so that i can delete it by earse(), in that question he needed a bool return. – Yassine Mrabet May 15 '20 at 13:47
  • std::find_if() indeed returns an iterator. Where do you need to return bool? – Kashinath Patekar May 15 '20 at 14:18

1 Answers1

1

The actual error is not in x.first == name but is in invocation of std::find() because name (which is std::string ) will be compared to every std::pair< std::string, double>

Instead of overloading the operator == itself, you may use the std::find_if() by passing it a lambda like this:

auto itr = std::find_if(melee_champ.begin(), melee_champ.end(), 
    [&name](pair<string, double> const& p) {
        return p.first == name;
    });
Kashinath Patekar
  • 1,083
  • 9
  • 23