Constructors of a class

This is a discussion on Constructors of a class within the C++ Programming forums, part of the General Programming Boards category; I'm trying to construct a class "Time" that included a separate custom library "Date". I understand that a single colon ...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    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!

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    Try this

    Time::Time( int hr, int min, int sec, int bmonth, int bday, int byear)
    : birthDate( bmonth, bday, byear )
    {
    setTime( hr, min, sec );
    }
    The experimenter who does not know what he is looking for will not understand what he finds.
    - Claude Bernard

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    4
    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.

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Talking

    I solved my own problem: nevermind

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  2. Replies: 8
    Last Post: 10-02-2005, 01:27 AM
  3. Replies: 1
    Last Post: 11-10-2002, 07:37 PM
  4. Constructors + Derived class
    By MethodMan in forum C++ Programming
    Replies: 6
    Last Post: 11-10-2002, 05:05 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21