ostream operator << error

Hi to everyone!

I am a novice programmer and I am studying Stroustrup's Book Programming: Principles and Practice using C++. I am now stuck with a code regarding how to create a Date class. I have checked my output with the one provided in the book and surely I miss a bug I cannot find. It regards an error in ostream operator << that alerts me it was never declared to handle Month enum class.

Here my code.

The two classes are:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  enum class Month {
	jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};

class Date {
public:
	class Error_Date { };
	void add_day(int n);
	Date(int y, Month mm, int d) :y{ y }, m{ mm }, d{ d } {};
	Date() :y{ 1900 }, m{ Month::jan }, d{ 1 } {};
	Month month() const { return m; }
	int year() const { return y; }
	int day() const { return d; }
private:
	int y, d;
	Month m;
};


Custom ostream operator << definition is:

1
2
3
4
5
6
7
8

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


In Visual Studio the error I have is: C2679.

Thank you very much

Leo
Looks like you forgot to declare operator<< in the header.
closed account (E0p9LyTq)
Your class doesn't know how to handle ostream. You might want to add an ostream operator<< declaration to your Month class as a public access friendly function in your class declaration.

Your class could be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
  enum class Month {
	jan = 1, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec
};

class Date {
public:
	class Error_Date { };
	void add_day(int n);
	Date(int y, Month mm, int d) :y{ y }, m{ mm }, d{ d } {};
	Date() :y{ 1900 }, m{ Month::jan }, d{ 1 } {};
	Month month() const { return m; }
	int year() const { return y; }
	int day() const { return d; }
        friend ostream& operator<< (ostream& os, const Date& d);
private:
	int y, d;
	Month m;
};
The VS must say more than just "C2679". It should say something like
binary 'operator' : no operator found which takes a right-hand operand of type 'type' (or there is no acceptable conversion)

Where the operator and type are some concrete names, like '<<' and 'enum class Month'.

Furthermore, the compiler should point the line, where the error occurs.


You do have os << d.month() and Date::month() does return type Month. What should that print?

The old enums do convert implicitly to integral types. The strongly typed enums do not.

You could use explicit conversion: os << static_cast<int>( d.month() )
Or, you could implement ostream& operator<< (ostream&, const Month&);
Thank you for your answers guys!

I have tried to add the friend ostream& operator<< (ostream& os, const Date& d); into my class but it is still alerting me about this error..

Once when I have implemented Month enum class it returned me this error about the operator <<.

In the previous version it worked fine!
You're right keskiverto, I read about C2679 and I have figured out this!

I will try to implement ostream& operator<< (ostream&, const Month&);.

Thanks!
Topic archived. No new replies allowed.