Search code examples
c++classoperator-overloadingfriend-function

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


I have an application to do that works with time data(hour, minutes, seconds).

Add in the class next operators: - (binary operator) defined as member function: it returns the difference between the two operands; if operand1 is smaller than operand2, it returns the time 0:0:0

There are working only print functions and toseconds() function.

This is the error:

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

Solution

  • *this is a time object, as well as '`' in the following section:

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

    You can't convert time variable type into 'long' variable type, unless you implement 'operator()' for long casting. If you don't want to overload the casting operator for type 'long', you can use a function to convert it for you (like your toseconds function, but it must to return the value, and not just print it).

    Without casting operator:

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

    With operator() overloading it'll looks like that:

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