user3683473
user3683473

Reputation: 37

Why must I use reference to return/pass std::ostream?

Why I got an error with this code :

ostream operator<<(ostream flux, Perso const A)
{
    A.O_Show(flux);
    return flux;
}
error: use of deleted function 'std::basic_ostream<char>::basic_ostream(const std::basic_ostream<char>&)'|

And no errors with :

ostream& operator<<(ostream& flux, Perso& const A)
{
    A.O_Show(flux);
    return flux;
}

Can you explain what's the difference?

Upvotes: 2

Views: 91

Answers (1)

πάντα ῥεῖ
πάντα ῥεῖ

Reputation: 1

As for your code

ostream operator<<(ostream flux, Perso const A) {
    A.O_Show(flux);
    return flux;
}

You cannot copy a std::ostream as return value (prior to standards, and even these are protected in 1st place), just change your code to

ostream& operator<<(ostream& flux, Perso& const A) {
    // ^
    A.O_Show(flux);
    return flux;
}

Upvotes: 2

Related Questions