help with Segmentation fault

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();


}

Last edited on
First of all, welcome:) And second, please use code-tags when posting pieces of code:) The segfault seems to be happening in the getTOD-function. A segfault usually occurs when you're trying to access something in memory which isn't there, so I'm guessing that somewhere along the line the timeofday-variable isn't being properly initialized.
Thans. but getTOD(),getHour() getMinute() functions are in my Time class

//Constructor
Time::Time(int h,int m, string tod): hour(h), min(m), timeofday(tod)

If it isn't being properly initialized as you said, how come getHour() and getMinute() functions are okay. they are almost same.
Topic archived. No new replies allowed.