c++ - 无法将 int 类型转换为时间类型(我的类类型)
问题描述
我有一个应用程序可以处理时间数据(小时、分钟、秒)。
在类中添加下一个运算符: - (二元运算符)定义为成员函数:它返回两个操作数之间的差异;如果操作数 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;
}
解决方案
*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;
}
您不能将time
变量类型转换为“long”变量类型,除非您实现“operator()”进行长转换。如果您不想为“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;
}
推荐阅读
- json - 如何在flutter中解析json列表
- c++ - 如何编译/构建/链接 - XLNT 库?
- asp.net-mvc - 带有 IdentityServer4 的 ASP.NET MVC 5:登录 url 和访问被拒绝
- scala - 如何通过迭代scala中的列表来添加转换后的对象进行设置
- swift - 我应该使用哪个路径上传到 Dropbox 中的团队文件夹
- javascript - VueJS将参数传递给弹出“对话框”
- javascript - 如何在 React JS 组件中预加载图像?
- amazon-web-services - 在哪里可以找到 Lambda@Edge 的日志?
- javascript - 使用带有 OpenStreetMaps 的 Leaflet 来勾勒建筑物
- c - make 失败,链接器抱怨重复符号