Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,617 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 986 people online right now. Registration is fast and FREE... Join Now!




Date class getting error

 
Reply to this topicStart new topic

Date class getting error

Kayoss
post 3 Apr, 2006 - 06:35 PM
Post #1


New D.I.C Head

*
Joined: 7 Mar, 2006
Posts: 8


My Contributions


This code is getting the following error:
"Date: multiple default constructors defined"
I have Date();
and Date(int=1,int=1,int=1990); in the header, but not sure why it's wrong?

CODE
#include <iostream>
#include <ctime>
#include <iomanip>

using namespace std;

class Date {

public:
  Date();
  Date( int = 1, int = 1, int = 1900 ); // default constructor
  void FormatTime();
  void print() const;  // print date in month/day/year format
  ~Date();  // provided to confirm destruction order

private:
  int month;  // 1-12 (January-December)
  int day;    // 1-31 based on month
  int year;   // any year
  int myTime;

 // utility function to test proper day for month and year
  int checkDay( int ) const;  

}; // end class Date  

Date::Date()
{
  myTime=time(0);  //to take the current time
  FormatTime();
  cout<<"The system Time is:"<<endl;
  cout<<endl;
}

// constructor confirms proper value for month; calls
// utility function checkDay to confirm proper value for day
Date::Date( int mn, int dy, int yr )
{
 if ( mn > 0 && mn <= 12 )  // validate the month
    month = mn;

 else {                     // invalid month set to 1
    month = 1;
    cout << "Month " << mn << " invalid. Set to month 1.\n";
 }

 year = yr;                 // should validate yr
 day = checkDay( dy );      // validate the day

 // output Date object to show when its constructor is called
 print();                    
 cout << endl;

}

void Date::FormatTime()
{
int century;
century = myTime/(60*60*24*30*12*100);
year=(myTime-(century*60*60*24*30*12*100))/(60*60*24*30*12);
month=(myTime-(century*60*60*24*30*12*100)-(year*60*60*24*30*12))/(60*60*24*30);
day=(myTime-(century*60*60*24*30*12*100)-(year*60*60*24*30*12)
-(month*60*60*24*30))/(60*60*24);

}


// print Date object in form month/day/year
void Date::print() const
{
cout <<setw(3)<<setfill('0')<<day<<" "<<year<<endl;//DDD YYYY
  cout <<setw(2)<<setfill('0')<<month<< '/'  
 <<setw(2)<<setfill('0')<<day << '/' << year%100<<endl;  //MM/DD/YY
switch(month)
{
case 1:
cout<<"January ";
break;
case 2:  
cout<<"February ";
break;
case 3:  
cout<<"March ";
break;
case 4:  
cout<<"April ";
break;
case 5:  
cout<<"May ";
break;
case 6:  
cout<<"June ";
break;
case 7:  
cout<<"July ";
break;
case 8:  
cout<<"August ";
break;
case 9:  
cout<<"September ";
break;
case 10:  
cout<<"October ";
break;
case 11:  
cout<<"November ";
break;
case 12:  
cout<<"December ";
break;
}//end switch

cout << day <<", "<< year << endl;  

} // end function print

// output Date object to show when its destructor is called
Date::~Date()
{  
} //empty destructor

// utility function to confirm proper day value based on  
// month and year; handles leap years, too
int Date::checkDay( int testDay ) const
{
 static const int daysPerMonth[ 13 ] =  
    { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

 // determine whether testDay is valid for specified month
 if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
    return testDay;

 // February 29 check for leap year  
 if ( month == 2 && testDay == 29 &&
    ( year % 400 == 0 ||                      
       ( year % 4 == 0 && year % 100 != 0 ) ) )
    return testDay;

 cout << "Day " << testDay << " invalid. Set to day 1.\n";

 return 1;  // leave object in consistent state if bad value

} // end function checkDay


int main()
{
Date D1;
D1.FormatTime();
D1.print();

Date D2(6,14,1992);  //month,day,year
cout<<endl;
Date D3(10,3);
cout<<endl;
return 0;
}



Thanks for the help!
User is offlineProfile CardPM

Go to the top of the page

sontek
post 4 Apr, 2006 - 02:57 AM
Post #2


D.I.C Regular

Group Icon
Joined: 13 Sep, 2001
Posts: 283



Thanked 1 times

Dream Kudos: 85
My Contributions


I fixed and cleaned up your code a bit... The problem was you had your default constructor with default values.. So both your constructors would work without passing arguments to them.


But please from now on format your code properly so it is easier for us all to read.
CODE

#include <iostream>
#include <ctime>
#include <iomanip>

using namespace std;

class Date {

public:
 Date( int = 1, int = 1, int = 1900 ); // default constructor
 void FormatTime();
 void print() const;  // print date in month/day/year format
 ~Date();  // provided to confirm destruction order

private:
 int month;  // 1-12 (January-December)
 int day;    // 1-31 based on month
 int year;   // any year
 int myTime;

// utility function to test proper day for month and year
 int checkDay( int ) const;  

}; // end class Date  

// constructor confirms proper value for month; calls
// utility function checkDay to confirm proper value for day
Date::Date( int mn, int dy, int yr )
{
    if ( mn > 0 && mn <= 12 )  // validate the month
       month = mn;
   
    else {                     // invalid month set to 1
       month = 1;
       cout << "Month " << mn << " invalid. Set to month 1.\n";
    }
   
    year = yr;                 // should validate yr
    day = checkDay( dy );      // validate the day
   
    // output Date object to show when its constructor is called
    print();                    
    cout << endl;

}

void Date::FormatTime()
{
   int century;
   century = myTime/(60*60*24*30*12*100);
   year    =(myTime-(century*60*60*24*30*12*100))/(60*60*24*30*12);
   month   =(myTime-(century*60*60*24*30*12*100)-(year*60*60*24*30*12))/(60*60*24*30);
   day     =(myTime-(century*60*60*24*30*12*100)-(year*60*60*24*30*12)-(month*60*60*24*30))/(60*60*24);
}


// print Date object in form month/day/year
void Date::print() const
{
    cout << setw(3)<<setfill('0')<< day <<" "<< year <<endl;//DDD YYYY
    cout << setw(2)<<setfill('0')<< month << '/'  
         << setw(2)<<setfill('0')<< day << '/' << year % 100 <<endl;  //MM/DD/YY
   switch(month)
   {
        case 1:
             cout<<"January ";
             break;
        case 2:  
             cout<<"February ";
             break;
        case 3:  
             cout<<"March ";
             break;
        case 4:  
             cout<<"April ";
             break;
        case 5:  
             cout<<"May ";
             break;
        case 6:  
             cout<<"June ";
             break;
        case 7:  
            cout<<"July ";
            break;
        case 8:  
            cout<<"August ";
            break;
        case 9:  
            cout<<"September ";
            break;
        case 10:  
            cout<<"October ";
            break;
        case 11:  
            cout<<"November ";
            break;
        case 12:  
            cout<<"December ";
            break;
   }//end switch

cout << day <<", "<< year << endl;  

} // end function print

// output Date object to show when its destructor is called
Date::~Date() {  } //empty destructor

// utility function to confirm proper day value based on  
// month and year; handles leap years, too
int Date::checkDay( int testDay ) const
{
   static const int daysPerMonth[ 13 ] =  
          { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

// determine whether testDay is valid for specified month
   if ( testDay > 0 && testDay <= daysPerMonth[ month ] )
      return testDay;

// February 29 check for leap year  
    if ( month == 2 && testDay == 29 && ( year % 400 == 0
         || ( year % 4 == 0  && year % 100 != 0  ) ) )
            return testDay;

    cout << "Day " << testDay << " invalid. Set to day 1.\n";

    return 1;  // leave object in consistent state if bad value

} // end function checkDay


int main()
{
   Date D1;
   D1.FormatTime();
   D1.print();
   
   Date D2(6,14,1992);  //month,day,year
   cout<<endl;
   Date D3(10,3);
   cout<<endl;
   return 0;
}



This post has been edited by sontek: 4 Apr, 2006 - 02:59 AM
User is offlineProfile CardPM

Go to the top of the page

Kayoss
post 4 Apr, 2006 - 10:34 AM
Post #3


New D.I.C Head

*
Joined: 7 Mar, 2006
Posts: 8


My Contributions


Thank you, I didn't realize that the Date() constructor was unnecessary for this program.
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:10AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month