Hello,
I need to provide 2 overloaded constructors, each one displaying a date in a different format. Below is my class implementations, header file and main() function code snippets. The code is followed by the compilation error I get. I have tried a number of things, mainly changing the order of my constructors and such, but I keep getting this error. Could I get some expert guidance please? There is a lot of code here; some could have been abridged more, but I color-coded the code in question for easy readability (HOPEFULLY easy for you)
THANKS!!!
// Date.cpp
// Date class member-function definitions.
#include <iostream>
using std::cout;
using std::endl;
#include "Date.h" // include Date class definition
// 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
{
month = 1; // invalid month set to 1
cout << "Invalid month (" << mn << ") set to 1.\n";
} // end else
year = yr; // could validate yr
day = checkDay( dy ); // validate the day
// output Date object to show when its constructor is called
cout << "Date object constructor for date ";
print();
cout << endl;
} // end Date constructor
Date:Date( const char mth, int yr ) // overloaded constructor for 2nd date format
{
monthW = mth;
year = yr;
}
Date:Date( const char mth , int dy, int yr ) // overloaded constructor for 3rd date format
{
monthW = mth;
day = dy;
year = yr;
}// print Date object in form month/day/year
void Date:print() const
{
cout << month << '/' << day << '/' << year;
} // end function print
// print Date object in format of January 1, 2000
void Date:rint2() const
{
cout<< month << "" << day << "," << year;
} // end function print2
// output Date object to show when its destructor is called
Date::~Date()
{
cout << "Date object destructor for date ";
print();
cout << endl;
} // end ~Date 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 << "Invalid day (" << testDay << ") set to 1.\n";
return 1; // leave object in consistent state if bad value
} // end function checkDay
//---------------------------------------------------------------------
// Date.h
// Date class definition; Member functions defined in Date.cpp
#ifndef DATE_H
#define DATE_H
class Date
{
public:
Date( int = 1, int = 1, int = 1900 ); // default constructor
Date( const char, int = 1900);//overloaded constructor
Date( const char, int = 1, int = 1900); // 2nd overloaded constructorvoid print() const; // print date in month/day/year format
void print2() const; // print date in a January, 1, 2000 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
char monthW; // month spelled out
// utility function to check if day is proper for month and year
int checkDay( int ) const;
}; // end class Date
#endif
//---------------------------------------------------------------------
// Fig. 10.14: fig10_14.cpp
// Demonstrating composition--an object with member objects.
#include <iostream>
using std::cout;
using std::endl;
#include <ctime>
using std::time;
#include "Employee.h" // Employee class definition
int main()
{
time_t date; // time_t object to hold the date
time(&date); //set date variable to current date
Date birth( 7, 24, 1949 ); // using constructor
Date hire( "July", 1988 ); // overloaded constructor (first one with different date format)
Employee manager( "Bob", "Blue", birth, hire );cout << endl;
manager.print();
cout<<"\nTest Date constructor with valid valuesn";
Date lastDayOff("December", 25, 2007); // overloaded constructor (second one with different date format)cout<<"Employee's last day off: \n";
lastDayOff.print2(); // print overloaded arguments
cout<<endl;
cout<<"Date of report: "<<ctime(&date)<<endl;
return 0;
} // end main
//-----------------------------------------------------------------------
1>cprogram files\c++examples\cpphtp6e_examples\ch10\fig10_10_ 14\fig10_14.cpp(19) : error C2664: 'Date:ate(int,int,int)' : cannot convert parameter 1 from 'const char [5]' to 'int'
1> There is no context in which this conversion is possible
1>cprogram files\c++examples\cpphtp6e_examples\ch10\fig10_10_ 14\fig10_14.cpp(26) : error C2664: 'Date:ate(int,int,int)' : cannot convert parameter 1 from 'const char [9]' to 'int'
1> There is no context in which this conversion is possible