0

mySongs is a vector that store a collection of songs input by the user. In the if statement, the program will check the elements in the vector with user input. If match, it will delete that specified value from the vector. When I look for the solution, I see someone recommend to use remove/erase idiom:. But when I implement in my code, it continue pop up this error C2678 binary '==': no operator found which takes a left - hand operand of type 'Song' (or there is no acceptable conversion)

void deleteSong() {
    string songTitle;

    cout << "\n\t\tPlease enter the particular song name to remove: ";
    cin >> songTitle;

    if (songTitle != "") {
        for (Song songs : mySongs) {
            if (songs.title == songTitle) {
                mySongs.erase(find(mySongs.begin, mySongs.end, songs));  //erase an element with value
                break;
            }
        }
    }
}
  • Read up on [operator overloading](https://en.cppreference.com/w/cpp/language/operators) for classes. Yes, you should employ the [erase-remove idiom](https://stackoverflow.com/questions/347441/erasing-elements-from-a-vector) when deleting elements from a vector. – Ron Jan 13 '21 at 07:51
  • @Ron can you explain more detailed about this **operator overloading**. I still not understand after look the website content that you share with me ? For the situation like me, is it add ``operator==`` in ```if (songs.title == songTitle)```? Am I right? –  Jan 13 '21 at 07:59
  • Operators work well on built-in types. But they need to be overloaded when applied to objects of classes. More info in [this SO post](https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading). – Ron Jan 13 '21 at 08:04

1 Answers1

0

This error is due to the fact that the class Song does not have an == operator. This can be solved in one of the two following ways

  1. If you have access to the source code of Song then add the following function to it. please note that const is necessary

     bool operator == ( const Song& song)
    {
         //Do the comparison code here
    }
    
  2. If you don't have access to the source code. Then add the following function

    bool operator == (const Song& song1, const Song& song2)
    {
         // Do the comparison code here  
    }
    

That said, there is another minor problem with your code

The erase function should be called like this

     mySongs.erase(find(mySongs.begin(), mySongs.end(), songs));

I decided to add a minimal example to help you. In that example I assumed a certain implementation of Song, but that implementation does not have to be what you have

    #include <iostream>
    #include<vector>
    #include<algorithm>
    #include<string>
    using namespace std;
    class Song
    {
        public:
        std::string title;
    /*  bool operator ==(const Song& other)
        {
            return this->title == other.title;
        }*/
    };
    bool operator ==(const Song& song1,const Song& other)
        {
            return song1.title == other.title;
        }
    std::vector<Song> mySongs={{"ali"},{"ahmed"},{"ali"}};;
    
    void deleteSong() {
        string songTitle;

        cout << "\n\t\tPlease enter the particular song name to remove: ";
        cin >> songTitle;

        if (songTitle != "") {
            for (const auto& songs : mySongs) {
                if (songs.title == songTitle) {
        
                    mySongs.erase(find(mySongs.begin(), mySongs.end(), songs));  //erase an element with value
                    break;
                }
            }
        }
    }
    int main() {

        // your code goes here
        return 0;
    }
Motaz Hammouda
  • 122
  • 1
  • 9
  • it pop up a message ```"operator==": must return a value. Why? –  Jan 13 '21 at 08:31
  • @KongJason you need to provide an implementation of the function and return `true` or `false`. We can't do that for you as we are lacking a [mre] with a definition of `Song` – Alan Birtles Jan 13 '21 at 08:35
  • Option 2 is possibly better replaced by using `find_if` rather than globally overriding the comparison operator for a third party class – Alan Birtles Jan 13 '21 at 08:36