我有一个应用程序可以处理时间数据(小时,分钟,秒)。
在类中添加下一个运算符: -(二进制运算符)定义为成员函数:它返回两个操作数之间的差;如果操作数1小于操作数2,则返回时间0:0:0
只有工作的打印功能和toseconds()函数。
这是错误:
Error 2 error C2440: 'type cast' : cannot convert from 'const time' to 'long' 47 1 timeex2
#include <iostream>
using namespace std;
class time {
int hour, min, sec;
void normalize(); // it transforms the sec and min values on the inside of
// [0,59] interval and hour values on the inside of
// [0, 23] interval.
// Ex: the time 25: 79: 80 is transformed in 2 : 20: 20
public:
time(int=0, int=0, int=0); // values of the members are normalized
void print1(); // print on the screen the values as hour : min : sec
void print2(); // print on the screen the values as hour : min : sec a.m. / p.m.
void operator-(const time&);
void toseconds() {
sec=3600*hour+60*min+sec;
cout << sec;
}
// friend time operator+(const time t) {
// time t1, t2, t3;
// t3 = t1 + t2;
// time::normalize();
// return t3;
// }
// friend time operator>(time t1, time t2) {
// toseconds(t1);
// toseconds(t2);
// if (t1 > t2)
// cout << "\nt1 is bigger\n";
// else
// cout << "\nt1 is smaller\n";
// }
// friend time operator==(time t1, time t2) {
// toseconds(t1);
// toseconds(t2);
// if (t1 == t2)
// cout << "\nEqual\n";
// else
// cout << "\nNot equal\n";
// }
};
void time::operator-(const time& t) {
long a = *this; // The error is here
long b = (long)t; // The error is here
if (a < b)
cout << "\n0:0:0\n";
else
cout << "\nThe difference is " << a-b << endl;
}
time::time(int a, int b, int c) {
hour = a;
min = b;
sec = c;
normalize();
}
void time::normalize() {
int s = sec;
int m = min;
int h = hour;
sec = s % 60;
min = (m + s/60) % 60;
hour = (h + m/60 + s/3600) % 24;
}
void time::print1() {
normalize();
cout << hour << ":" << min << ":" << sec << endl;
}
void time::print2() {
normalize();
if (hour >= 13)
cout << hour%12 << ":" << min << ":" << sec << " p.m." << endl;
else
cout << hour << ":" << min << ":" << sec << " a.m." << endl;
}
int main() {
time t1(12,45,30), t2(0,0,54620), t3;
t1.print1();
t2.print1();
t1.print2();
t2.print2();
cout << "\nTime t1 to seconds\n";
t1.toseconds();
t1.operator-(t2);
cin.get();
return 0;
}
答案 0 :(得分:2)
*this
是时间对象,在以下部分中也是“`”:
void time::operator-(const time& t) {
long a = *this; // convert *this to long
long b = (long) t; // convert t to long
if (a < b)
cout << "\n0:0:0\n";
else
cout << "\nThe difference is " << a - b << endl;
}
除非将'operator()'用于长类型转换,否则不能将time
变量类型转换为'long'变量类型。如果您不想重载类型为'long'的转换运算符,可以使用一个函数为您转换(例如toseconds
函数,但它必须返回值,而不仅仅是打印它) )。
没有转换运算符:
class time {
private:
// ...
public:
// ...
long to_seconds() const { // the const is necessary so you will be able to use this method ovet the t parameter in the operator- function (because t defined as `const time&`)
auto local_sec = 3600 * hour + 60 * min + sec;
// cout sec; // print the value
return local_sec; // return the value
}
// ...
}
void time::operator-(const time& t) {
long a = this->to_seconds(); // take the long value from *this object
long b = t.to_seconds(); // take the long value from t object
if (a < b)
cout << "\n0:0:0\n";
else
cout << "\nThe difference is " << a - b << " seconds" << endl;
}
operator()
超载时,它看起来像这样:
class time {
private:
// ...
public:
// ...
operator long() const; // Declare operator overloading for `long` type
long to_seconds() const { // the const is necessary so you will be able to use this method ovet the t parameter in the operator- function (because t defined as `const time&`)
auto local_sec = 3600 * hour + 60 * min + sec;
// cout sec; // print the value
return local_sec; // return the value
}
// ...
}
time::operator long() const {
return to_seconds(); // return the desired long value in cast procedure
}
void time::operator-(const time& t) {
long a = *this; // cast *this object from `time` type into `long` type
long b = t; // cast t object from `time` type into `long` type
if (a < b)
cout << "\n0:0:0\n";
else
cout << "\nThe difference is " << a - b << " seconds" << endl;
}
答案 1 :(得分:2)
通常,运算符应返回结果:
time time::operator-(const time& t);
然后在运算符内部,将其强制转换为long
:
long a=*this;
long b=(long)t;
唯一:没有任何这样的演员!
此类强制转换仅在本地存在,从一种基本数据类型(int
,unsigned long
,char
,double
等)到另一种。如果涉及其他任何数据类型,则必须显式定义强制转换运算符。
所以: 班级 { 上市: 显式运算符long() { 返回3600L 小时+ 60L min +秒; // ^ ^ //使用长字面量确保小时和分钟将被转换 //与乘法一样长-然后与其他求和 //已经很长了,秒也将转换为。 } };
有了它,您现在可以 转换为空。顺便说一句:explicit
关键字确保您需要显式强制转换,否则,它将在适当的上下文中隐式应用:
time t;
long l0 = t; // possible only without keyword
long l1 = static_cast<long>(t); // required with (alternatively C-style cast)
现在,您将构造一个新的时间对象以返回结果:
a -= b;
return a < 0 ? time(0, 0, 0) : time(a / 3600, a / 60 % 60, a % 60);
运算符不应输出任何内容,相反,您应该对返回的结果进行操作。
但是,一个问题是,根据结果operator-
,您无法区分两个时间值之前是否相等,或者第一个运算符较小。
因此,您需要先比较 :
time t1, t2;
if(t1 < t2)
std::cout << "\n0:0:0\n";
else
std::cout << "\nThe difference is " << static_cast<long>(t1 - t2) << std::endl;
如您所见,任何输出都在操作符之外 完成的。当然,这需要适当定义的bool time::operator<(time const& other);
或独立变量bool operator<(time const& x, time const& y);
(这是我个人更喜欢的一种。