Hi;

can you check this for me :

Time Time::operator-( Time & other, Time & other2)
{
 Time t3;
 t3.hrs=fabs(other.hrs-other2.hrs);
 t3.mins=fabs(other.mins-other2.mins);
 t3.secs=fabs(other.secs-other2.secs);
 return t3;
}
bool Time::operator!=(const Time &other)
{
 return (hrs!=other.hrs || mins!=other.mins || secs!=other.secs);
}

this error apear while runing the prog.

: error C2804: binary 'operator -' has too many parameters
: error C2676: binary '-' : 'class Time' does not define this operator or a conversion to a type acceptable to the predefined operator
: error C2511: '-' : overloaded member function 'class Time (class Time &,class Time &)' not found in 'Time'
 see declaration of 'Time'

this in main :

t1.settime(2,13,12);
 t1.gettime(hrs,mins,secs);
 t2.settime(4,17,12);
 t2.gettime(hrs,mins,secs);
 t3=t1-t2;
 
 t3.gettime(hrs,mins,secs);

Dani AI

Generated

The compiler errors come from a mismatch between the function you defined and the operator form the compiler expects. As noted by and , a member overload has an implicit this and therefore takes one explicit parameter; the two-parameter form is only valid for a non-member (usually friend) operator. Also make parameters and methods const-correct and avoid using fabs (that is for floating-point).

A safe, practical implementation is to convert times to total seconds, do the subtraction, then convert back. This avoids incorrect per-field borrowing (seconds/minutes) and lets you decide whether you want a signed result or an absolute difference. Example declarations and a member implementation:

// in Time class (header)
class Time {
public:
    Time operator-(const Time &other) const;   // member form
    bool operator!=(const Time &other) const;
private:
    int hrs, mins, secs;
};

// in Time.cpp
Time Time::operator-(const Time &other) const {
    int a = hrs*3600 + mins*60 + secs;
    int b = other.hrs*3600 + other.mins*60 + other.secs;
    int d = a - b;           // signed difference
    bool negative = d < 0;
    if (negative) d = -d;    // remove sign for magnitude result

    Time res;
    res.hrs = d / 3600;
    res.mins = (d % 3600) / 60;
    res.secs = d % 60;
    return res;              // if you need the sign, return seconds or add a flag
}

If you prefer a free function, declare it as a friend with two const Time& parameters (that is the two-parameter form showed). Quick troubleshooting checklist: make sure the declaration in the class exactly matches your definition (C2511), use const Time& instead of non-const references, mark member operators const when appropriate, and replace fabs with integer math or std::abs for ints.

Recommended Answers

All 5 Replies

You are doing operator overloading the wrong way. The function needs to take only one parameter, not two, since one of them would be the instance on which the function is called.

Read some good operator overloading tutorials.

That is true, but let us not forget that you can also do friend operator overloading with a rhs and lhs parameters -

namespace twomers
{
    class coord
    {
    private:
        int x, y;

    public:
        coord ()
        {}

        coord( const int n_x, const int n_y )
            : x( n_x ), y( n_y )
        {}

        friend std::ostream &operator << ( std::ostream &os, const twomers::coord &inst )
        {
            return os << "x: " << inst.x << ", y: " << inst.y << std::endl;
        }

//        twomers::coord operator + ( const coord &param ) // Have either this commented
//        {
//            coord temp( x+param.x, y+param.y );            
//            return temp;
//        }
        friend coord operator + ( const coord &rhs, const coord &lhs ); // or this
    };

    twomers::coord operator + ( const coord &rhs, const coord &lhs ) // and this
    {
        coord temp( lhs.x+rhs.x, lhs.y+rhs.y );            
        return temp;
    }
}

int main( void )
{
    twomers::coord x( 1, 1 );
    twomers::coord y( 3, 4 );
    twomers::coord s;

    s = x+y;

    std::cout<< x << y << s;

    return 0;
}

Just another method if you wanted to use the two-parameter model instead of the single one for some reason.

True, but considering that his operator overloading basics are a bit off the mark, it would only mean adding to his confusion by mentioning the other ways.

coord( const int n_x, const int n_y )
            : x( n_x ), y( n_y )
        {}

Primitives are anyways passed by value. The const keyword is an overkill IMHO.

That is true, but let us not forget that you can also do friend operator overloading with a rhs and lhs parameters

True, but the error was because it should only be one.

I know. But I was informing the OP that there is an option to use a rhs and lhs parameter if he really wanted to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.