Program received signal SIGSEGV, Segmentation fault.
0x001c74a5 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) () from /usr/lib/libstdc++.so.6
(gdb) where
#0 0x001c74a5 in std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) () from /usr/lib/libstdc++.so.6
#1 0x0804b444 in Time::getTOD (this=0x0) at Time.cpp:63
#2 0x0804a4f0 in main (argc=2, argv=0xbffff7a4) at printsongs.cpp:102
(gdb) frame 1
#1 0x0804b444 in Time::getTOD (this=0x0) at Time.cpp:63
63 return timeofday;
(gdb) frame 2
#2 0x0804a4f0 in main (argc=2, argv=0xbffff7a4) at printsongs.cpp:102
102 << T->getMinute() << " " << T->getTOD() << endl;
(gdb)
Here are the codes:
Song s = songs.getNthSong (n);
Song defaultSong;
// Print out the last time it was played
if (!(s == defaultSong) ) {
Time *T = s.getLastPlayed();
cout << "Song #" << n << " is " << s.getTitle() << " by "
<< s.getArtist() << ". Last played at " << T->getHour() << ":"
<< T->getMinute() << " " << T->getTOD() << endl;
}
string Time::getTOD()
{
return timeofday;
}
This error happens when I add the operator= method
Song& Song::operator =(const Song &so){
if(last_played==0){
last_played=0;
}
else{
if(last_played!=0){
delete last_played;
}
last_played=new Time (*so.getLastPlayed());
}
artist=so.getArtist();
title=so.getTitle();
}