Erase element from <set>
Jun 21, 2013 at 9:40am UTC
Hello, I am having a problem erasing elemnet from set. I'm getting 11 errors which send me to "xstddef standard header". I have no idea what have i done wrong with it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
class Song{
public :
enum eSection{ INTRO, VERSE, CHORUS, TRANSITION };
enum eSectionDurationInSeconds{ INTRO_DUR = 5, VERSE_DUR = 10, CHORUS_DUR = 20, TRANSITION_DUR = 5 };
Song(){ m_songName = "Not Set Yet" ; m_singerName = "Not Set Yet" ; m_composerName = "Not Set Yet" ; } //Default Constructor
Song(string songName, string composerName, string singerName);//Constructor
~Song();//Destructor
void set(); // ?
string getSongName();
void addSection(const eSection sec, const eSectionDurationInSeconds iDuration);
const void playSong() const ;// Function Plays the Song.
bool operator == (const Song& s) const ;
void operator ()(const int & sec);
const double getSongLenghtInMinutes() const ;
private :
string m_songName; // Song Name.
string m_composerName; // Song Composer
string m_singerName; // The singer, could be more than one.
vector<pair<eSection,eSectionDurationInSeconds>> m_songStructure;
};
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#ifndef RadioStation_h
#define RadioStation_h
#include"Song.h"
#include"Playlist.h"
#include<vector>
#include<set>
class RadioStation{
private :
vector<Playlist> m_RadioPlayList;
set<Song> m_Db;
int m_numOfLists;
double m_totalListsTime;
public :
enum {MAX_PLAYLIST_LEN_IN_MINUTES = 12};
RadioStation() { m_numOfLists = 0; m_totalListsTime = 0; }
~RadioStation();
//bool isRadioListEmpty() { if( m_numOfLists==0 ) return true; else false; }
void addSongToDB(const Song& newsong);
void removeSongFromDB(const Song& song);
void addPlaylist(const Playlist& pl);
void removePlaylist(unsigned int iPlaylistNumber);
void playAllPlaylists();
};
i saw erase from set on this site, but it didnt work for me.
this part doesnt work..
1 2 3
void RadioStation::removeSongFromDB(const Song& song){
m_Db.erase(song);
}
I also tried to use iterators, but it did't work either.
Last edited on Jun 21, 2013 at 9:41am UTC
Jun 21, 2013 at 10:10am UTC
std::set uses operator< by default so if you have not overloaded operator< for Song you will get an error because of that.
Jun 21, 2013 at 10:13am UTC
std::set requires a "less than" function (operator) to be available.
In your case, you must carefully overload
operator< for
Song .
1 2 3 4 5 6 7 8 9 10 11 12 13
bool operator < (const Song &s) const
{
if (m_songName != s.m_songName)
return m_songName < s.m_songName;
else
if ( /* composer */ )
return /* ... */ ;
else
if ( /* singer name */ )
return /* ... */ ;
else
return m_songStructure < s.m_songStructure;
}
Of course, the else branches aren't needed because
return quits the function altogether -- they're just there to clarify the intent.
Topic archived. No new replies allowed.