0

I'm learning about class composition but I'm having a hard time understanding how the syntax works. I have two classes, Time and Date, and Date is composed with a Time object. I can't get the Date constructor to work correctly - there is a compiler error stating "default argument : cannot convert from 'int' to 'Time' and I'm not sure how to correctly set this up. I'm trying to pass a default value of (0, 0, 0) to the Time object.

Here's my two class headers. The error is appearing on the Date constructor definition line.

Date header:

#ifndef DATE_H
#define DATE_H

#include "Time.h"

class Date 
{
public:
   static const unsigned int monthsPerYear = 12; // months in a year
   Date( int = 1, int = 1, int = 1900, Time = (0, 0, 0)); // <- ERROR with this line
   ~Date(); // provided to confirm destruction order
   void print() const; // print date in month/day/year format
   void tick();      // function that increments seconds by 1.
   void increaseADay(); // increases the day by one
private:
   unsigned int month; // 1-12 (January-December)
   unsigned int day; // 1-31 based on month
   unsigned int year; // any year
   Time time;  // private Time object - class composition
   // utility function to check if day is proper for month and year
   unsigned int checkDay( int ); 
}; // end class Date

#endif

Time header:

#ifndef TIME_H
#define TIME_H

// Time class definition
class Time 
{
public:
   explicit Time( int = 0, int = 0, int = 0 ); // default constructor
   ~Time();  // destructor
   // set functions
   void setTime( int, int, int ); // set hour, minute, second
   void setHour( int ); // set hour (after validation)
   void setMinute( int ); // set minute (after validation)
   void setSecond( int ); // set second (after validation)

   // get functions
   unsigned int getHour() const; // return hour
   unsigned int getMinute() const; // return minute
   unsigned int getSecond() const; // return second

   void printUniversal() const; // output time in universal-time format
   void printStandard() const; // output time in standard-time format
private:
   unsigned int hour; // 0 - 23 (24-hour clock format)
   unsigned int minute; // 0 - 59
   unsigned int second; // 0 - 59
}; // end class Time

#endif

Within my Date implementation file, this is where I'm not sure how to deal with the Time constructor, and this is most likely where my error is. I wrote my Date constructor like this:

Date::Date( int mn, int dy, int yr, Time time)
{
    // some validation code
}

And my Time constructor looks like this:

Time::Time( int hour, int minute, int second ) 
{ 
    // some validation code
}
Sabien
  • 219
  • 2
  • 16

1 Answers1

3

Try Date( int = 1, int = 1, int = 1900, Time = Time(0, 0, 0));

The syntax (0,0,0) is just a comma separated list of ints surrounded by brackets, not a Time object.

Because the default constructor is the same as the argument list you provided you could also do this Date( int = 1, int = 1, int = 1900, Time = Time());

Dominique McDonnell
  • 2,510
  • 16
  • 25