Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Operator Problem

I don't know if this question is already answered on stackoverflow. But I simply can not find the right keyword to search for.

I inserted some stripped down version of my code below.

So basically what I'm trying to do in my main(), is to subtract 122 from t2. I suppose my compiler converts the integer to a Timestamp object automatically and then subtracts it as showed in 'Timestamp.cpp'.

But when it arrives at t4 it doesn't convert it and give me the following error:

no match for 'operator-' in '722 - t1'

I'm 100% sure that it is possible. But how?

Maybe I'm totally wrong about converting... So please do hesitate to correct me, I'm trying to learn something.

STRIPPED DOWN CODE:

main.cpp:

#include <iostream>
#include <iomanip>

#include "Timestamp.h"

using namespace std;

int main() {
    Timestamp t3(t2 - 122);
    cout << "T3 = " << t3 << endl;
    Timestamp t4(722 - t1);
    cout << "T4 = " << t4 << endl;

    return 0;
}

Timestamp.h

#ifndef TIJDSDUUR_H
#define TIJDSDUUR_H

using namespace std;

class Timestamp {
    public:
        Timestamp(int);
        Timestamp operator- (const Timestamp &t);
    private:
        int hour;
        int min;
};

Timestamp.cpp

Timestamp::Timestamp(int m) : hour(0), min(m) {

}

Timestamp Timestamp::operator- (const Timestamp &t) {
    Timestamp temp;

    temp.hour = hour;
    temp.min = min;

    temp.hour -= t.hour;
    temp.min -= t.min;

    while(temp.min < 0.00) {
        temp.hour--;
        temp.min += 60;
    }

    return temp;
}
like image 225
Ian van Wijk Avatar asked Sep 28 '11 15:09

Ian van Wijk


People also ask

Why is (++ i )++ an error in C?

The expression ++i++ is evaluated as ++(i++) which is illegal in C as the postfix increment returns a value and prefix incrementing on a that value makes no sense. What you have is somewhat equivalent to: ++(5) which is obviously illegal as you can apply prefix increment on something that's not a l-value.

What is === operator in C?

=== is not an operator in C. It is used in other languages, such as PHP. In PHP, === checks for value and type. – Sarwar Erfan.


2 Answers

Contrary to what the other answers propose, you do not need to provide an specialized operator- that takes an int and a Timestamp, rather you can (and probably should) make operator- a non-member function:

Timestamp operator-( Timestamp lhs, Timestamp const & rhs ); // Returns a timestamp?? really??

That way the compiler is free to apply conversions to both the left hand side and the right hand side operands, and use the implicit conversion from int to Timestamp.

You can read a short description on design and implementation of operator overloads here, or you can search in the [C++-faq] tag in SO for "operator overload".

like image 56
David Rodríguez - dribeas Avatar answered Oct 27 '22 13:10

David Rodríguez - dribeas


You need to define the following non-member function:

Timestamp operator-(int left, const Timestamp &right)
{
    //define
}

By the way, make the member operator-() const function as:

Timestamp operator-(const Timestamp &t) const;
                                       //^^^^^ this makes the function const

If you make this const, only the right-left would work, because right is const object, and so only const member function can be invoked on it.


I noticed that you've a constructor that takes int as parameter. This constructor can behave like implicit conversion function from int to TimeStamp which means, you don't need to define two operator-() functions (one as member function which you already have defined, and other as non-member function as I suggested above). Instead, you can define only one friend function of this type:

Timestamp operator-(const Timestamp &left, const Timestamp &right)
{
  //define
}

You've to make friend of the class, as it needs to access the private members of the class. However, @David provided a better solution. But I'm keeping this solution only for academic purpose.

like image 44
Nawaz Avatar answered Oct 27 '22 14:10

Nawaz