Search code examples
c++classc++11operator-overloadingfriend-function

How friend function works


I wanted to learn how to use friend functions. When attempting it for the first time, I have little problem and I don't know how to fix it. I got the following errors:

|17|error: 'minutes' was not declared in this scope|
|18|error: 'hours' was not declared in this scope|
|24|error: 'minutes' was not declared in this scope|
|24|error: 'minutes' was not declared in this scope|

Here's is all the code I have as of now:

#include <iostream>
using namespace std;

class Time
{

    int hours;
    int minutes;
    friend Time operator+(const Time & t);
    friend void x(Time h, Time m );

};

Time operator+(const Time & t)
{
    Time sum;
    sum.minutes = minutes + t.minutes;
    sum.hours = hours + t.hours + sum.minutes / 60;
    sum.minutes %= 60;
    return sum;
}


void x(Time h, Time m) {hours = h; minutes = m;}

Solution

  • These error messages

    |17|error: 'minutes' was not declared in this scope|

    |18|error: 'hours' was not declared in this scope|

    means that within this function definition

    Time operator+(const Time & t)
    {
        Time sum;
        sum.minutes = minutes + t.minutes;
                      ^^^^^^^
        sum.hours = hours + t.hours + sum.minutes / 60;
                    ^^^^^
        sum.minutes %= 60;
        return sum;
    }
    

    the variables minutes and hours are not declared. The function is not a member function of the class. So these variables are not data members of an object of the class Time. They are undeclared identifiers.

    Friend functions do not get the implicit argument this as non-static member class functions do.

    These error messages

    |24|error: 'minutes' was not declared in this scope|

    |24|error: 'minutes' was not declared in this scope|

    have the same meaning. The function x is not a member function of the class Time.

    If the friend function operator + overloads the binary operator + then it shall have two parameters.

    As for the second friend function then it seems its task is to set values for an object of the type Time.

    The friend functions should be declared and defined the following way as it is shown in the demonstrative program below.

    #include <iostream>
    #include <iomanip>
    
    class Time
    {
        int hours;
        int minutes;
        friend Time operator +( const Time &t1, const Time &t2 );
        friend void x( Time &t, int h, int m );
        friend std::ostream & operator <<( std::ostream &is, const Time &t );
    };
    
    Time operator +( const Time &t1, const Time &t2 )
    {
        const int HOURS_IN_DAY = 24;
        const int MINUTES_IN_HOUR = 60;
    
        Time t;
    
        t.hours  = t1.hours + t2.hours + ( t1.minutes + t2.minutes ) / MINUTES_IN_HOUR;
        t.hours %= HOURS_IN_DAY;
        t.minutes = ( t1.minutes + t2.minutes ) % MINUTES_IN_HOUR;
    
        return t;
    }
    
    void x( Time &t, int h, int m )
    {
        t.hours = h;
        t.minutes = m;
    }
    
    std::ostream & operator <<( std::ostream &os, const Time &t )
    {
        return 
        os << std::setw( 2 ) << std::setfill( '0' ) << t.hours 
           << ':'
           << std::setw( 2 ) << std::setfill( '0' ) << t.minutes; 
    }
    
    int main() 
    {
        Time t1;
    
        x( t1, 16, 10 );
    
        std::cout << t1 << '\n';
    
        Time t2;
    
        x( t2, 10, 20 );
    
        std::cout << t2 << '\n';
    
        std::cout << t1 + t2 << '\n';
    
        return 0;
    }
    

    The program output is

    16:10
    10:20
    02:30