How Can I overload operator >> and << correctly with cout and cin fucntions

How Can I overload operator >> and << correctly with cout and cin fucntions

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

#include <iostream>
#include <fstream>
using namespace std;

class Date {
private:
	unsigned int year;
	unsigned int month;
	unsigned int day;
public:
	Date() {
		year = 0;
		month = 12;
		day = 31;
	}
	Date(int y, int m, int d): year(y), month(m), day(d){		
	}
	Date& operator()(int hour, int min, int sec);
	int getDay()const  {
		return day;
	}
	int getMonth()const  {
		return month;
	}
	int getYear()const  {
		return year;
	}
	void increaseDay() {
		if (day == 31) {
			day = 1;
			month++;
		}
		else if (month == 12 && day ==31) {
			year++;
			day = 1;
			month = 1;
		}
		else {
			day++;
		}
	}
	Date& operator>>() {

	}
	Date& operator<<() {
		
	}
	Date& operator--() {
		if (day == 1) {
			day = 31;
			month--;
		}
		else if (month == 1 && day == 1) {
			year--;
			day = 31;
			month = 12;
		}
		else {
			day--;
		}
		return *this;
	}
	Date& operator++() {
		if (day == 31) {
			day = 1;
			month++;
		}
		else if (month == 12 && day == 31) {
			year++;
			day = 1;
			month = 1;
		}
		else {
			day++;
		}
		return *this;
	}

	Date& operator=(Date&obj){
		year = obj.year;
		month = obj.month;
		day = obj.day;
	}
	Date& operator +=(const Date&obj) {
		this->year += obj.year;
		this->month += obj.month;
		this->day += obj.day;
		return *this;
	 }
	Date& operator -=(const Date&obj) {
		this->year -= obj.year;
		this->month -= obj.month;
		this->day -= obj.day;
		return *this;
	}
	
};

Date& Date::operator()(int y, int m, int d) {
	this->year = y;
	this->month = m;
	this->day = d;
	return *this;
}

inline bool operator >(const Date&a, const Date&b) {
	if (a.getYear() > b.getYear() && a.getMonth() > b.getMonth() && a.getDay() > b.getDay()) {
		return true;
	}
	return false;
}
inline bool operator <(const Date&a, const Date&b) {
	if (a.getYear() < b.getYear() && a.getMonth() < b.getMonth() && a.getDay() < b.getDay()) {
		return true;
	}
	return false;
}

inline bool operator ==(const Date&a, const Date&b) {
	if (a.getYear() == b.getYear() && a.getMonth() == b.getMonth() && a.getDay() == b.getDay()) {
		return true;
	}
	return false;
}
inline bool operator!=(const Date&a, const Date&b) {
	return !(a == b);
}

int main() {
	Date k;
	cout << k.getDay()<<endl;
	k.increaseDay();
	cout << k.getDay();

	cin.get();
}
Look at how those operators are used:
1
2
std::cin >> foo;
std::cout << foo;

These are binary operators; two operands -- left and right.

If you write a member operator, it takes the right operand as argument and the class object is the left operand. Left operand of << and >> is iostream and you cannot add members to iostream.

You have to write the overloads as standalone functions.
See http://en.cppreference.com/w/cpp/language/operators
it shows:
1
2
3
4
5
6
7
8
9
10
11
12
13
std::ostream& operator<<(std::ostream& os, const T& obj)
{
    // write obj to stream
    return os;
}

std::istream& operator>>(std::istream& is, T& obj)
{
    // read obj from stream
    if( /* T could not be constructed */ )
        is.setstate(std::ios::failbit);
    return is;
}

Since these operators are not members' they have to either use public interface of the class, or be friends of the class.
Topic archived. No new replies allowed.