为什么我必须使用引用返回/传递std :: ostream?

时间:2014-06-04 22:22:10

标签: c++

为什么我的代码出错:

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>&)'|

没有错误:

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

你能解释一下有什么区别吗?

1 个答案:

答案 0 :(得分:2)

至于你的代码

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

您无法将std::ostream复制为返回值(标准之前,甚至第一位protected),只需将代码更改为

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