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

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

C++ Friend Functions, too many functions or external classes are declared as friends of a class with protected or private data, it lessens the value of encapsulation of� 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.

Friend class and function in C++, This page contains tutorial about friend function and friend class in C++ To make this program work properly, a forward declaration of a class class B should � Friend functions are not considered class members; they are normal external functions that are given special access privileges. Friends are not in the class's scope, and they are not called using the member-selection operators (. and ->) unless they are members of another class. A friend function is declared by the class that is granting access.

When you write:

class Time
{
    friend Time operator+(const Time & t);
};

Then operator+ is not a member function. It is a free function and the line in the class declaration only declares that this function is a friend of the class Time.

As a non-member, the binary operator+ has to take 2 parameters. Consider how you would use it:

Time a,b;
Time c = a + b;

You need to pass a and b and return a new Time c:

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

You have similar problem with x, but I don't understand what it is supposed to do. If you understand the issue with operator+ you should also be able to fix that.

C++ Friend Functions and Friend Classes: A Complete Guide , In object-oriented programming, a friend function, that is a "friend" of a given class, is a function that is given the same access as methods to private and� A friend function is a function that can access the private members of a class as though it were a member of that class. In all other regards, the friend function is just like a normal function. A friend function may be either a normal function, or a member function of another class.

Friend function, Function Class Example. In this example we have two classes XYZ and ABC . The XYZ class has two private data members ch� friend Function in C++ If a function is defined as a friend function then, the private and protected data of a class can be accessed using the function. The complier knows a given function is a friend function by the use of the keyword friend.

Friend Class and Friend Functions in C++, We will first go through the theory of what is a friend function ? and later we will see a practical Duration: 10:03 Posted: Jan 8, 2018 How Friend function works I wanted to test the friend function for the first time but I got error: class Time { int hours; int minutes; friend Time operator+

Friend Functions in C++ Programming, A C++ friend functions are special functions which can access the private It works!! hemed on May 18th, 2011: why the argument or parameter of a friend� Friend Functions In C++ Friend Function In C++. A friend function in C++ is a function that is preceded by the keyword “friend”. When the Example Of A Friend Function. Let us implement a programming Example to better understand the usage of Friend Function. Friend Class. Just like friend

Comments
  • If you define operator+ as a non-member function, it must take 2 parameters.
  • when you write sum.minutes = minutes + t.minutes; then what do you think minutes refers to?
  • friend functions are not members, just regular functions. Once you get that, all errors should make sense.
  • @CássioRenan One can have friends that are member functions of a different class.
  • @CássioRenan Just wanted to point out that "friend functions are not members, just regular functions" is not a correct statement in general.
  • It is possible the OP is confusing member functions with friend functions.
  • @Yakk-AdamNevraumont It seems he thinks that friend functions behave as non-static member functions.
  • @formerlyknownas_463035818: The unary version is used as the positive sign of the object; if it is declared as a member function then it must take 0 argument otherwise if as friend then it must take an argument. But the OP here doesn't know that he/she is implementing the unary version not the binary one which he/she intends.
  • A non-member + can take 1 argument. I'm positive! coliru.stacked-crooked.com/a/8d59f01be2f0f8d3 It does, however, not add up. It is just positive. Like, the opposite of negative.