无法将 int 类型转换为时间类型(我的 class 类型)

Can t transform int type to time type(my class type)

我有一个应用程序可以处理时间数据(小时、分钟、秒)。

在class下添加运算符: - (二元运算符)定义为成员函数:它returns两个操作数之间的差异;如果 operand1 小于 operand2,则 returns 时间 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 函数,但它必须 return 值,而不仅仅是打印出来)。

没有转换运算符:

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;
}

通常,运算符应该return一个结果:

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

然后在你的运算符中,你转换为 long:

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

唯一:没有这样的转换运算符!

这种类型转换仅存在于从一种基本数据类型(intunsigned longchardouble、...)到另一种基本数据类型的情况下。如果涉及任何其他数据类型,则必须显式定义转换运算符。

所以: class我 { public: 显式运算符 long() { return3600L小时+60L分钟+秒; // ^ ^ // 使用长文字确保小时和分钟将被转换 // 和乘法之前一样长 - 然后和其他被加数一样 // 已经很长了,sec 也将被转换为。 } };

这样,您现在 可以 转换为 void。顺便说一下:explicit 关键字确保您需要显式转换,否则,它将在适当的上下文中隐式应用:

time t;
long l0 = t;                    // possible only without keyword
long l1 = static_cast<long>(t); // required with (alternatively C-style cast)

现在您将构建一个新的时间对象,结果是 return:

a -= b;
return a < 0 ? time(0, 0, 0) : time(a / 3600, a / 60 % 60, a % 60);

运算符不应该输出任何东西,相反,你会在结果上这样做 returned.

但是,一个问题是,从结果 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); 或者 free-standing变体 bool operator<(time const& x, time const& y);(这是我个人更喜欢的那个)。