The friend is needed in order to make the function global. If you omit it, the function would be considered member function, in which case it should get only one parameter, and the caller should be of type Train (the class type in which we are declared), which doesn't fit our needs.
You can use in your case a global function instead:
class Train{ public: char direction; int loading_time, crossing_time; ... }; std::ostream& operator<<(std::ostream& os, const Train& t){ os << t.direction << '/' << t.loading_time << '/' << t.crossing_time; return os; }
However, it is very common to use a friend function for this use case (instead of a non-friend global function), as it is declared inside the class, which is an important side bonus that you get.
friendis needed because the function is (inappropriately) defined inside the class. Withoutfriendit would be a member function, with a different signature.friendis not needed in this case.friendgrants access to a class's private members. This function does not need such access.friendserves no purpose and violates encapsulation. It's not needed and I don't recommend it.