error while overloading << operator

Hello,

just getting started with classes. Have a Date class, using enumeration to define months. Then I overload operator << to print out date of "today" and get the following error:


error: cannot bind 'std::basic_ostream<char>' lvalue to 'std::basic_ostream<char>&&'|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\4.9.2\include\c++\ostream|602|note: initializing argument 1 of 'std::basic_ostream<_CharT, _Traits>& std::operator<<(std::basic_ostream<_CharT, _Traits>&&, const _Tp&) [with _CharT = char; _Traits = std::char_traits<char>; _Tp = Month]'|


The error happened after I defined Month as enum. Before I had it defined among private as an int and all worked well.


[code]
enum class Month{
jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};

class Date
{
int year; // year
Month m; // month
int day; //day of the month
public:
void add_day(int n);
Date(int y, Month m, int d);
ostream& operator<<(ostream& os, const Date& d);
}


ostream& operator<<(ostream& os, const Date& d)
{
os << '(' << d.year << ',' <<d.month()<< ',' << d.day << ')';

return os;
}

int main()
{
Date today = Date(1978, Month::feb, 25);
cout << today ;

}
;
Last edited on
The definition of class Date does not end }; ... there is only a }
At the end of the code you have unnecessary ;

Fixing those we get to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>

enum class Month{
  jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};

class Date
{
  int year; // year
  Month m; // month
  int day; //day of the month
public:
  void add_day(int n);
  Date(int y, Month m, int d);
  std::ostream& operator<<(std::ostream& os, const Date& d);
};


std::ostream& operator<<(std::ostream& os, const Date& d)
{
  os << '(' << d.year << ',' <<d.month()<< ',' << d.day << ')';
  return os;
}

int main()
{
  Date today = Date(1978, Month::feb, 25);
  std::cout << today ;
}

15:59: error: 'std::ostream& Date::operator<<(std::ostream&, const Date&)' must take exactly one argument
 In function 'std::ostream& operator<<(std::ostream&, const Date&)':
9:7: error: 'int Date::year' is private
21:18: error: within this context
21:34: error: 'const class Date' has no member named 'month'
11:7: error: 'int Date::day' is private
21:53: error: within this context

Line 15 declares a member function of Date and with wrong number of arguments.
Lines 19-23 declare and define a standalone function; different from line 15.

Perhaps you were trying to declare the standalone as a friend of the class?
If you want to declare operator<< as a friend you need to use the friend keyword.

 
friend ostream& operator<<(ostream& os, const Date& d);


You need a semicolon at the end of the class definition.

1
2
3
4
class Date
{
	...
};


The Date class doesn't have member function named month(). If you want to print the numerical value you can cast m to an int.

 
os << '(' << d.year << ',' << static_cast<int>(d.m) << ',' << d.day << ')';
Thank you for the help - it's working now!


Implemented all the corrections, testing it after each one. The one that really did it was using <static_cast>.

Last edited on
Topic archived. No new replies allowed.