Trouble with overloading the << operator

Back again ladies and gentlemen.

Below is a cpp file from an assignment. The program consists of three files, th lcock.h, the clock.cpp, and the main.cpp. My objective is to make a clock object that can be modified. The objective was to practice with operator overdloading, and we got to design the program. I understand the clock isnt practical, but for the assignment it works. BUT! I digress.

Im simply having issues with my third overloading, of the << operator. It is currently commented out. And yes, I understand that Im missing more elements, memory leaks, etc.

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
#include <iostream>
#include "clock.h"
using namespace std;

Clock::Clock() {
	mHour = new int;
	mMinutes = new int;
	*mHour = 0;
	*mMinutes = 0;
}

Clock::Clock(int hour) {
	mHour = new int;

	*mHour = hour % 12;
}

Clock::Clock(int hour, int minutes) {
	mHour = new int;
	mMinutes = new int;

	*mHour = hour % 12;
	*mHour += minutes/60;
	*mMinutes = minutes % 60;
}

int Clock::getHour() {
	return *mHour;
}

int Clock::getMinutes() {
	return *mMinutes;
}

void Clock::setHour(int hour) {
	*mHour = hour;
}

void Clock::setMinutes(int minutes) {
	*mMinutes = minutes;
}

Clock Clock::operator+(int minutes) {
	Clock result(*mHour, *mMinutes + minutes);
	return result;
}

Clock Clock::operator+(const Clock &other) {
	Clock result(*mHour + *other.mHour,
		*mMinutes + *other.mMinutes);
	return result;
}

Clock Clock::operator-(int minutes) {
	Clock result(*mHour, *mMinutes - minutes);
	return result;
}

// memberwise assignment
void Clock::operator=(const Clock &rhs) {
	*mHour = *rhs.mHour;
	*mMinutes = *rhs.mMinutes;
}

/*ostream &operator<<(ostream &output, Clock &m) {
	output << *m.mHour << "";
	if (*m.mMinutes < 10) {
		output << "0";
	}
	output << *m.mMinutes ;
	return output;
}*/
what is the error that you are getting?
There is no reason to be calling new and using pointers in that code.

1
2
3
4
5
6
7
8
9
ostream &operator<<(ostream &output, const Clock& m) 
{
    output << m.getHour() << ':' ;

	if (m.getMinutes() < 10)
		output << '0';

    return output << m.getMinutes() ;
}
Topic archived. No new replies allowed.