ostream& operator<<(ostream& out, String const& s) { ostream& out << ss; return out; } 

s - char *

Error: Reference to type 'std :: basic_ostream' must be initialized

How to write correctly?

    1 answer 1

    ostream& out << ss; You say: out is a link to ostream (link to which object?) This is a newly announced link, although it has the same name as the function argument. The argument in already said that this is a reference to an object of type ostream , which you pass to the function. So just use it:

     out << ss; 

    and further:

    not String const& s , but const String& s

    • Regarding "more": people have a different style; The one that you indicated is no better than what the author used. - ixSci
    • @ixSci, basically you are right, but there are such things as readability and common style ... - AR Hovsepyan
    • There is no such thing as a “conventional style”, at least in C ++. And this option is often found in code in people, because String const& corresponds to the type reading rule in C ++: a reference to a constant String. - ixSci