无法将 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 biggern";
//      else
//          cout << "nt1 is smallern";
//  }
//  friend time operator==(time t1, time t2) {
//      toseconds(t1);
//      toseconds(t2);
//      if (t1 == t2)
//          cout << "nEqualn";
//      else
//          cout << "nNot equaln";
//  }
};
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:0n";
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 secondsn";
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:0n";
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:0n";
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:0n";
else
cout << "nThe difference is " << a - b << " seconds" << endl;
}

通常,运算符应返回一个结果:

time time::operator-(const time& t);

然后在您的运算符中,您投射到long

long a=*this;
long b=(long)t;

唯一:没有这样的演员!

这种强制转换只存在于从一种基本数据类型(intunsigned longchardouble等)到另一种基本数据类型。如果涉及任何其他数据类型,则必须显式定义强制转换运算符。

所以: 类 IME { 公共: 显式运算符 long() { 返回3600L小时+60L分钟+秒;// ^ ^ 使用长文字可确保转换小时和分钟 在乘法之前也长 - 然后作为其他求和 已经很长了,SEC也将转换为。 } };

有了这个,你现在可以投射到空化。顺便说一下: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:0n";
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);(这是我个人更喜欢的变体)。

最新更新