开发者

C++ Operator Problem

I don't know if this question is already answered on stackoverflow. But I simply can not find the right keyword 开发者_C百科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;
}


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".


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.


Two things I see:

First, your constructor is taking a parameter of type int. Your operator is returning type TimeStamp, which you're then passing to the constructor. You haven't specified a conversion from TimeStamp to int, so this won't work!

Second, your operator is taking a parameter of type timestamp, but you appear to subtracting an int from it when you're actually using it. It has no idea what an int is though, because you didn't tell it!


Your Timestamp::operator-(int) allows you to subtract an integer from a Timestamp object. It does not allow you to subtract Timestamps from integers.

You need to add operator- as a free function

Timestamp operator-(int, const Timestamp&);

This needs to be a friend of Timestamp as it needs to access the private members.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜