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

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.