can i declare operator overloading function as a friend function?
i did it and i get errors

#include <iostream.h>
class time
{
    int h;
    int m;
    friend time operator +(time);


    public:
    void input();
    time();
    time(int,int);

    void display();
};
time::time()
{
    h=0;
    m=0;
}

time::time(int a,int b)
{
    h=a;
    m=b;
}

void time::input()
{
    cout<<"Enter hours :";
    cin>>h;
    cout<<"\nEnter Minutes :";
    cin>>m;
}

void time::display()
{
    cout<<"Hours :"<<h;
    cout<<"\nMinutes :"<<m;
}

time operator +(time a)
{
    time tmp;
    tmp.h=h+a.h;
    tmp.m=m+a.m;
    if(tmp.m>=60)
    {
        tmp.h+=(tmp.m/60);
        tmp.m=tmp.m%60;
    }

    return tmp;
}

int main()
{
    time t1(10,40),t2(5,30),t3;
    t3=t1+t2;
    t3.display();
    return 0;
}

errors

[Error] C:\Users\Shane\Documents\C-Free\Temp\Untitled1.cpp:49: `h' undeclared (first use this function)
[Error] C:\Users\Shane\Documents\C-Free\Temp\Untitled1.cpp:49: (Each undeclared identifier is reported only once
[Error] C:\Users\Shane\Documents\C-Free\Temp\Untitled1.cpp:49: for each function it appears in.)
[Error] C:\Users\Shane\Documents\C-Free\Temp\Untitled1.cpp:50: `m' undeclared (first use this function)
[Error] C:\Users\Shane\Documents\C-Free\Temp\Untitled1.cpp:64: no match for `time & + time &'
[Error] C:\Users\Shane\Documents\C-Free\Temp\Untitled1.cpp:47: candidates are: class time operator +(time)

Edited 3 Years Ago by mike_2000_17: Fixed formatting

can i declare operator overloading function as a friend function?

You can, but you need function woth two parameters:

friend time operator + (time& left, time& right);

Edited 6 Years Ago by volvo877: n/a

This article has been dead for over six months. Start a new discussion instead.