-
Constructors of a class
I'm trying to construct a class "Time" that included a separate custom library "Date". I understand that a single colon after the Time constructor passes the rest of the arguments ( bmonth, bday, byear) to the constructors of the Date data members/objects, which is a member of class "Time", but it hasn't been working on me.
What is the proper syntax for this constructor of mixed classes?
Time::Time( int hr, int min, int sec, int bmonth, int bday, int byear ) //Arguments
{ setTime( hr, min, sec ); }
//Arguments for functions in Time class
: birthDate( bmonth, bday, byear )
//colon passes the rest of the arguments to functions in the "Date" functions, defined elsewhere, but declared in the Time class
My actual code looks like this:
Time::Time( int hr, int min, int sec, int bmonth, int bday, int byear ) { setTime( hr, min, sec ); } : birthDate( bmonth, bday, byear )
I tried every reasonable order between the three. Help!
-
Try this
Time::Time( int hr, int min, int sec, int bmonth, int bday, int byear)
: birthDate( bmonth, bday, byear )
{
setTime( hr, min, sec );
}
-
I have tried that before and the compiler gave me three errors:
1. birthDate is not a base or member of Time - illegal member initialization.
2. birthDate is undeclared
3. birthDate must have a class/struct/union type
Its obvious that I'm having trouble with the compiler recognizing birthDate, which is a member of Date, but is defined in Time as const Date birthDate.
-
I solved my own problem: nevermind