質問

it has been for several days that I kept on my problem with no answer...

I'm trying to search an item in order to modify it. With a "list" I need to overload the operator== but I don't understand my mistake. Can you please tell me how can I solve this ?

class Nation{
private :
    short continent;
    unsigned int population, superficie;
    string pays, ville;
public :
list<Nation>    lireRemplir(list<Nation>liste, const char nomALire[]);

Nation(short continent, unsigned int population, unsigned int superficie, string pays, string ville) {
    ..... // ok
    }
Nation(){};
void modifierContinent(list<Nation> liste, string nomPays, short nouveauContinent);
bool operator == (Nation &); //?

};

bool Nation::operator == (Nation & autre) {
    return this->pays == autre.pays;
}

void modifierContinent(list<Nation> liste, string nomPays, short nouveauContinent)
{
    //Nation uneNation(0,0,0,nomPays,"");
    for (list<Nation>::iterator il = liste.begin(); il != liste.end(); il++)
    {
        if (*il == nomPays){ cout << "found!"; }    
    }
}

int main()
{
    list<Nation>liste;
    liste=lireRemplir(liste, "Nation.txt"); //hidden but working
    modifierContinent(liste, "FRANCE", 5);
}
役に立ちましたか?

解決

Here:

if (*il == nomPays){ cout << "found!"; } 

nomPays is type string, yet you overloaded the operator for another Nation type. There is no overloaded = that takes Nation and string.

Two solutions:

You can either overload a conversion to string (not recommended) or create a constructor that takes string.

The best solution is to create a getter method for pays just to do il->getPays() == nomPays. Clear and concise.

他のヒント

You do not necessarily have to overload an operator in your class. Here is why: You already use std::list. That's good. Now go one step further and also ditch your self-made algorithm in favour of the standard one: std::find_if.

std::find_if can search a standard container class using a comparison functor provided by you. Often, that functor is a struct with an overloaded operator() (so objects of it can be used like functions).

I'll give you an example:

#include <algorithm> // for std::find_if

// ...

struct CountryComparison
{
    CountryComparison(std::string const &country) : m_country(country) {}

    bool operator()(Nation const &nation) const
    {
        return nation.pays == m_country;
    }

    std::string m_country;
};

void modifierContinent(list<Nation> &liste, string const &nomPays, short nouveauContinent)
{
    list<Nation>::const_iterator find_iter = std::find_if(liste.begin(), liste.end(),
        CountryComparison(nomPays));

    if (find_iter != liste.end())
    {
        cout << "found!";
    }
}

I've also made sure strings are passed by const&, which should be the default for string arguments at least pre-C++11. And I pass liste by &, which is also more likely the intended behaviour (as it does not create needless copies).

By the way, your Nation class is strange. It contains a "country" (pays) and a "town" (ville). This means that in your class design, a nation consists of a country and a town. That doesn't make sense, except maybe for city states ;)


Edit: I forgot an implementation detail. As the functor cannot directly access the pays member of Nation, consider giving your class a member function like:

std::string GetPays() const
{
    return pays;
}

Or make the functor a friend of Nation.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top