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

标签 c++ class operator-overloading friend-function

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

在类 next 运算符中添加: - (二元运算符)定义为成员函数:它返回两个操作数之间的差值;如果 operand1 小于 operand2,则返回时间 0:0:0

只有 print 函数和 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;
}

关于c++ - 无法将 int 类型转换为时间类型(我的类(class)类型),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56715003/

相关文章:

Python __init__ 参数问题

php - 有没有正确的方法来处理类里面的数据库连接?

封装所有原语的 Java 对象——在 OutputStreams 中很有用

c# - 添加运算符重载以比较两个不同的对象。现在无法检查 null

C++ 未找到 << 运算符,它采用右手操作数

c++ - Qt, C++, 如何退出 QThread

c++ - 前进或 move 与否;如何确定在类的使用上下文中哪个是首选?

c++ - OpenCV KNearest 输入

c++ - 如何从父类中的函数调用子类的重载运算符,该函数将对父类的引用作为参数?

c++ - 使用反向迭代器从 vector 中快速删除