operator >> overloading not working

Hi, im doing exercises in B.Stroustrup's book "Programming principles and practice using C++" and this is part of code that should work. He didnt yet talk much about input/output streams but already included >> and << operator overloading in this code. Output operator << works fine but im getting
Error 1 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'Chrono::Date' (or there is no acceptable conversion)
this error when im trying to run this code if i also include operator >>. Can anyone explain me why is this happening?

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <string>

using namespace std;

namespace Chrono{

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

	class Date{					// Date class
	public:
		class Invalid{};

		Date(int y, Month m, int d); // constructor
		Date(); // defaut constructor

		//nonmodifying operations
		int day() const { return d; }
		Month month() const { return m; }
		int year() const { return y; }

		//modifying operations
		void add_day(int n);
		void add_month(int n);
		void add_year(int n);

	private:
		int y;
		Month m;
		int d;
	};

	Date::Date(int yy, Month mm, int dd) :y{ yy }, m{ mm }, d{ dd } {} // constructor
	

	const Date& default_date(){			//default date for default constructor						
		static Date dd{ 2001, Month::jan, 1 };
		return dd;
	}

	Date::Date()								// default constructor
		:y{ default_date().year() },
		m{ default_date().month() },
		d{ default_date().day() }
	{}

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

	std::istream& operator>>(std::istream& is, Date&& dd){  // >> operator
		int y, m, d;
		char ch1, ch2, ch3, ch4;
		is >> ch1 >> y >> ch2 >> m >> ch3 >> d >> ch4;
		if (!is) return is;
		if (ch1 != '(' || ch2 != ',' || ch3 != ',' || ch4 != ')'){
			is.clear(std::ios_base::failbit);
			return is;
		}
		dd = Date(y, Month(m), d);  // update dd
		return is;
	}
}


int main(){

	Chrono::Date my_date;
	cout << "enter date ";
	cin >> my_date;
	cout << "date you entered - " << my_date << endl;

}
Last edited on
1
2
//std::istream& operator>>(std::istream& is, Date&& dd)
std::istream& operator>>(std::istream& is, Date& dd)
Ohh man, didnt see that! Thank you a lot man :)
Topic archived. No new replies allowed.