How friend works in a struct (for ostream operator <<)? c++
I was trying to understand how overload << works, but I'm not sure how this works inside Entry struct: friend std::ostream& operator << (std::ostream& os, const Entry& e) { os << "{\"" << e.name << "\"," << e.number << "}"; return os; } cppreference says: "The friend declaration appears in a class body and grants a function or another class access to private and protected members of the class where the friend declaration appears. " So... why do I need "friend" to make the operator << works inside the struct when Entry's members are public? I tried multiple attempts: The attempt 1 it was like that because there is another << operator overloaded (for Dog struct). The attempt 2 is there because the compilers tells me there is an error (because attempt 1). Attempt 3 is there because I thought "maybe I don't need const Entry& because is implicit in the struct, but it didn't work; finally attempt 4 works. Why do I need friend inside the struct for << overloading? Here is the code where I show the multiple attempts: https://code.sololearn.com/ca4a14a9A6A2