Prev: function to plot coordinates
Next: Object oriented c++?
From: Tim on 15 Jul 2010 11:35 I'm new to c++ and I've been following some videos on Youtube. One of the videos discusses a time class which is shown below. The problem I have is that I'm having trouble trying to call adjust_time() from within the time constructor or from within the add_time() method. I can call the adjust_time method in the main() function and the output is what I wanted. I'm using VC 2008 to compile the code. There's no compile errors. #include <iostream> using namespace std; class time { private: int d; int h; int m; int s; int days; int hours; int minutes; int seconds; public: time(int d, int h, int m, int s); ~time(); void add_time(int d, int h, int m, int s); void adjust_time(); void output(); }; time::time(int d, int h, int m, int s) { days = d; hours = h; minutes = m; seconds = s; void adjust_time(); //this doesn't seem to be called } time::~time() { } void time::add_time(int d, int h, int m, int s) { days += d; hours += h; minutes += m; seconds += s; void adjust_time(); //this doesn't seem to be called } void time::adjust_time() { while (seconds >= 60) { seconds -=60; minutes += 1; } while (minutes >= 60) { minutes -=60; hours += 1; } while (hours >=24) { hours -= 24; days += 1; } } void time::output() { cout << days << " days, " << hours << " hours, " << minutes << " minutes, " << seconds << " seconds." << endl; } int _tmain(int argc, _TCHAR* argv[]) { time t(0,1,50,26); t.output(); t.add_time(0,12,3,55); /* t.adjust_time(); this gives the desired output, but I want to call from elsewhere t.output(); t.add_time(0,15,5,30); t.adjust_time(); t.output(); */ char c; cin >> c; return 0; } sdfds sdfsdf --- news://freenews.netfront.net/ - complaints: news(a)netfront.net ---
From: Francesco S. Carta on 15 Jul 2010 11:55 Tim <nospam(a)hotmail.com>, on 15/07/2010 16:35:10, wrote: > I'm new to c++ and I've been following some videos on Youtube. One of the > videos discusses a time class which is shown below. The problem I have is > that I'm having trouble trying to call adjust_time() from within the time > constructor or from within the add_time() method. <snip> > void adjust_time(); //this doesn't seem to be called The above is not a call, but a declaration, remove that "void" to transform it into a real call. Have fun continuing to learn C++ :-) -- FSC - http://userscripts.org/scripts/show/59948 http://fscode.altervista.org - http://sardinias.com
From: Ben Bacarisse on 15 Jul 2010 11:58 "Tim" <nospam(a)hotmail.com> writes: > I'm new to c++ and I've been following some videos on Youtube. One of the > videos discusses a time class which is shown below. The problem I have is > that I'm having trouble trying to call adjust_time() from within the time > constructor or from within the add_time() method. > > I can call the adjust_time method in the main() function and the output is > what I wanted. > > I'm using VC 2008 to compile the code. There's no compile errors. > > #include <iostream> > > using namespace std; > > class time > > { > > private: > > int d; > > int h; > > int m; > > int s; What are these for? You class member functions don't use them. If they are in the tutorial example, find another tutorial! BTW, try to get the layout right when you post code. I don't know if this "all on the left" comes from how you posted the code, but it should not be written that way for readability. Also all the extra blank lines make it harder to read. I've deleted them. > int days; > int hours; > int minutes; > int seconds; > > public: > time(int d, int h, int m, int s); > ~time(); > > void add_time(int d, int h, int m, int s); > void adjust_time(); > void output(); > }; > > time::time(int d, int h, int m, int s) > { > days = d; > hours = h; > minutes = m; > seconds = s; > > void adjust_time(); //this doesn't seem to be called This is not a function call -- it is a declaration. A call just uses the name (i.e. without the "void" return type). > } <snip> -- Ben.
From: neil on 15 Jul 2010 14:42 My understanding is that you can call a function within a function but you cannot define a function within a function, I'm not sure why though.
From: Keith Thompson on 15 Jul 2010 14:44
"Tim" <nospam(a)hotmail.com> writes: [...] > time::time(int d, int h, int m, int s) > > { > > days = d; > > hours = h; > > minutes = m; > > seconds = s; > > void adjust_time(); //this doesn't seem to be called > > > } That's because it's a declaration, not a function call. Try dropping the "void" keyword. BTW, did you really format your code that way (double-spaced with no indentation), or did your news software mess it up? Here's how I'd write it: time::time(int d, int h, int m, int s) { days = d; hours = h; minutes = m; seconds = s; adjust_time(); } (Or, depending on my mood and/or local coding standards, I might put the "{" on the same line as the declaration.) -- Keith Thompson (The_Other_Keith) kst-u(a)mib.org <http://www.ghoti.net/~kst> Nokia "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister" |