overloading, scalar multiplication

Hi Guys,

Need a little help. The issue is with the "operator*(Time& t)." I am trying to simply multiple a time object by a scalar double, in this case 3.0. However, I am not sure what's I am doing wrong as the compiler is not recognizing the operation
scalar=t1*scale. I suspect it has something to do with the constructor.

Thanks,

Mike

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
class Time
{
public:
	Time();
	Time(int h, int m, int s);
	Time(double d);
	void input();
	void print()const;
	Time operator+(Time& t);
	Time operator-(Time& t);
	Time operator++();
	Time operator++(int);
	Time operator--();
	Time operator--(int);
	Time operator*(Time& t);
private:
	int hour, minutes, seconds;
};
int main()
{
	char choice;
	Time t1(1,1,1), t2(23,60,60), addition, subtraction, scalar;
	double scale=3.0;
	do
	{
		cout<<"\n\n";
		//t1.input();
		//t2.input();
		cout<<"Time 1 is: ";
		t1.print();
		cout<<"Time 2 is: ";
		t2.print();

		addition=t1+t2;
		cout<<"Addition yeilds: ";
		addition.print();
		cout<<endl;

		subtraction=t1-t2;
		cout<<"Subtraction yeilds: ";
		subtraction.print();

		scalar=t1*scale;////////problem here!!
		scalar.print();

		cout<<"Again (y/n): ";
		cin>>choice;
	}while(choice=='y');
	cout<<"Have a nice day!";
	exit(1);

	return 0;
	_getch();
}
Time::Time(){}
Time::Time(int h, int m, int s):hour(h), minutes(m), seconds(s){}
Time::Time(double d):hour(d),minutes(d),seconds(d){}
Time Time::operator*(Time& t)////////problem here!!
{
	int h=hour*t.hour;
	int m=minutes*t.minutes;
	int s=seconds*t.seconds;
	return Time(h,m,s);////////problem here!!
}
Time Time::operator--(int)
{
	--seconds;
	if(seconds<1){seconds=60;--minutes;}
	if(minutes<1){minutes=60;--hour;}
	return Time(hour,minutes,seconds);
}
Time Time::operator--()
{
	seconds--;
	if(seconds<1){seconds=60;minutes--;}
	if(minutes<1){minutes=60;hour--;}
	return Time(hour,minutes,seconds);
}
Time Time::operator++(int)
{
	++seconds;
	if(seconds>60){seconds=0;++minutes;}
	if(minutes>60){minutes=0;++hour;}
	return Time(hour,minutes,seconds);	
	
}
Time Time::operator++()
{
	seconds++;
	if(seconds>60){seconds=0;minutes++;}
	if(minutes>60){minutes=0;hour++;}
	return Time(hour,minutes,seconds);
}
Time Time::operator-(Time& t)
{
	int s=seconds-t.seconds;
	if(s<0){s=60+s;}

	int m=minutes-t.minutes;
	if(m<0){m=60+m;}
	
	int h=hour-t.hour;
	if(h<0){h=24+h;}

	return Time(h,m,s);
}
Time Time::operator+(Time& t)
{
	int h=hour+t.hour;
	int m=minutes+t.minutes;
	int s=seconds+t.seconds;

	if(s>60){m++;s=s%60;}
	if(m>60){h++;m=m%60;}
	return Time(h,m,s);
}
void Time::print()const
{
	(hour<10)?cout<<"0"<<hour<<":":cout<<hour<<":";
	(minutes<10)?cout<<"0"<<minutes<<":":cout<<minutes<<":";
	(seconds<10)?cout<<"0"<<seconds:cout<<seconds;
	(hour>0 && hour<12)?cout<<" am":cout<<" pm";
	cout<<endl;
}
void Time::input()
{
	char symbol;
	cout<<"Enter time in the following format 'hh:mm:ss: '";
	cin>>hour>>symbol>>minutes>>symbol>>seconds;
	while(symbol!=':' || hour<0 || minutes<0 || seconds<0 || hour>24 ||minutes>60 || seconds>60)
	{
		cout<<"Incorrect parameters, enter again: ";
		cin>>hour>>symbol>>minutes>>symbol>>seconds;
	}
	cout<<endl;
}
see in line 15. You have defined operator* for your time class with argument also a time object pointer while you are calling the operator* with a double object which is not there.

If in your code you change in line 43 with t1=t1*t1 it works fine as suggested by my explanation above.

I tried your code with another operator* with argument double and return value Time and it compiled fine.
Thanks for the help. I can't run the program now (don't have access to a compiler) but will try it later tonight.

Mike
Topic archived. No new replies allowed.