Overload operator with this pointer

Not understanding why this overload of << will not allow this pointer to be used.

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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//ClockType.h

#include <iostream>
using namespace std;

class ClockType {
private:
	int hrs;
	int mins;
	int secs;
public:
	ClockType();
	ClockType(int hrs, int mins, int secs);
	~ClockType();
	void setTime(int hrs, int mins, int secs);
	void incrementSeconds();
	void incrementMinutes();
	void incrementHours();
	friend ostream & operator << (ostream & stream, ClockType & clocktype);
	bool operator == (ClockType & clocktype);
};

//ClockType.cpp

#include "ClockType.h"

ClockType::ClockType()
{
	this->hrs = 0;
	this->mins = 0;
	this->secs = 0;
}

ClockType::ClockType(int hrs, int mins, int secs)
{
	this->hrs = hrs;
	this->mins = mins;
	this->secs = secs;
}

ClockType::~ClockType()
{

}

void ClockType::setTime(int hrs, int mins, int secs)
{
	this->hrs = hrs;
	this->mins = mins;
	this->secs = secs;
}

void ClockType::incrementSeconds()
{
	this->secs++;
	if (this->secs == 60)
	{
		this->secs = 0;
		incrementMinutes();
	}
}

void ClockType::incrementMinutes()
{
	this->mins++;
	if (this->mins == 60)
	{
		this->mins = 0;
		incrementHours();
	}
}

void ClockType::incrementHours()
{
	this->hrs++;
	if (this->hrs == 25)
	{
		this->hrs = 0;
		this->mins = 0;
		this->secs = 0;
	}
}

ostream & operator << (ostream & stream, ClockType & clocktype)
{
	stream << this->hrs << "/"
		<< this->mins << "/"
		<< this->secs;

	return stream;
}

bool ClockType::operator == (ClockType & clocktype)
{
	if (hrs == clocktype.hrs)
	{
		if (mins == clocktype.mins)
		{
			if (secs == clocktype.secs)
			{
				return true;
			}
		}
	}
	else
		false;
}

//main.cpp

#include "ClockType.h"

void askTime(int &, int &, int &);

int main()
{
	ClockType mine;
	ClockType yours;
	int hrs, mins, secs;

	cout << "Set clock for my clock: ";
	askTime(hrs, mins, secs);
	mine.setTime(hrs, mins, secs);

	cout << "Set clock for your clock: ";
	askTime(hrs, mins, secs);
	yours.setTime(hrs, mins, secs);

	cout << "\n\n";

	cout << "The time on my clock is " << mine;

	if (mine == yours)
		cout << "The times of my clock and your clock are equal." << endl;
	else
		cout << "The times of my clock and your clock are not equal." << endl;


	
	return 0;
}

void askTime(int & hrs, int & mins, int & secs)
{
	cout << "Hour: ";
	cin >> hrs;
	cout << "Minute: ";
	cin >> mins;
	cout << "Second: ";
	cin >> secs;
}
A friend function is not part of the encompassing class. Instead you pass clocktype in order to use that:
1
2
3
4
5
6
7
8
ostream & operator << (ostream & stream, ClockType & clocktype)
{
	stream << clocktype.hrs << "/"
		<< clocktype.mins << "/"
		<< clocktype.secs;

	return stream;
}
operator<< isn't a member function so you've got to use the instance name followed by a dot (clocktype. in your case) rather than this-> to access the member variables:

1
2
3
4
5
6
7
8
ostream & operator << (ostream & stream, ClockType & clocktype)
{
	stream << clocktype.hrs << ":" // was /, but that's for dates!
		<< clocktype.mins << ":"
		<< clocktype.secs;

	return stream;
}


Andy

PS If you had commented out your #include lines

//#include "ClockType.h" -- not needed as contents of .h cut & pasted above

then your file would have compiled ("correctly" failed to compile) in C++ Shell without editing.
Last edited on
Topic archived. No new replies allowed.