I've got a class called Date, in which I have built several overloaded operators, including the << operator for cout statements. The date class takes three integers, day, month, and year, as arguments, verifies them, and is supposed to display the date in this format:
September 25, 2012
I've output a date in this format before, by sending the three date ints and passing them to a string object that contained an array of the names of all the months, then casting day and year as string, and outputting month with the array, like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
string Date::dsplyDate2()
{
string MonthStr[] = {"",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
string date2;
ostringstream convertDay;
convertDay << day;
ostringstream convertYr;
convertYr << year;
date2 = MonthStr[month] + " " + convertDay.str() + ", " + convertYr.str();
|
Now I've tried to do something similar using an overloaded << operator, but the compiler isn't liking it. Overloaded operators are a brand new concept for me, but here's what I tried.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
string Date::monthName()
{
string MonthStr[] = {"",
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December"};
string monthname;
monthname = MonthStr[month];
return monthname;
|
1 2 3 4 5
|
ostream &operator << (ostream &strm, const Date &obj)
{
strm << obj.monthName() << "," << obj.day << obj.year;
return strm;
}
|
Basically I tried to call this function in my overloaded output operator, but it isn't working. I tried to build the array of months in the operator itself, but I couldn't get it to work. Can anyone give me an idea of how to output a date in this format with an overloaded << operator?