Displaying string and int using an overloaded << operator

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?
You forgot to mark the monthName() function with const.
1
2
3
string Date::monthName() const
{
	...
First, a note. the MonthStr[] array should be declared static const. This array will never change, and never needs to change. You might also want to make it a static member of the class rather than defined within the function so it can be used in other member functions of the class, but that's not terribly important for this simple example.

As @Peter87 said, the function should be const, but that's not going to give you an error.

You said it's not working, but you didn't say how. Is it not compiling? Is it giving the wrong results when run? Is it crashing? You need to give us more information from which to diagnose your problem.

Based on the limited info I have, I suspect that you made monthName() a private function of the Date class, and you did not make the operator<<() a friend function. The operator<<() is not a member of the Date class, so if it needs to access private data or member functions, it needs to be made a friend.
Sorry, guys, didn't mean to leave out vital information like that. Let me address each point:

monthName() is a public member of the date class that I created to try and output the date in the proper format.

The error I'm getting is for obj in the strm << obj.monthName() << ", " << obj.day << obj.year. The program is underlining obj and telling me this: "the object has type qualifiers that are not compatible with the member function."

I actually did code my overloaded << and >> operators as friends. Let me show you the code for them.
Date.h
1
2
3
4
5
class Date; //Forward declaration

//Function prototypes for overloaded stream operators
ostream &operator << (ostream &, const Date &);
istream &operator >> (istream &, Date &);


1
2
3
//Friends
	friend ostream &operator << (ostream &, const Date &);
	friend istream &operator >> (istream &, Date &);


It isn't compiling, but it wouldn't because this is just a small part of a larger program using my Date class. I only posted the areas I need help on because I didn't want it to seem like I'm trying to get people to do this for me, and because my main.cpp needs to be rewritten from the last time I used the Date class.

I appreciate the help. C++ has been hard to grasp for me, and I'm still getting the hang of overloaded operators and how to use them. I hope this additional info is enough to work with.
"the object has type qualifiers that are not compatible with the member function."


... meaning the method is not const, but the object you receive in your overloaded operator<< is.
All right, all errors are gone. Gonna test this and see if I can't get the rest of the way on my own.

I appreciate all the help!
Topic archived. No new replies allowed.