为什么我的代码出现错误:
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;
}
你能解释一下有什么区别吗?
最佳答案
至于你的代码
ostream operator<<(ostream flux, Perso const A) {
A.O_Show(flux);
return flux;
}
您无法复制 std::ostream
作为返回值(在 c++11 标准之前,甚至这些在第一名也是 protected ),只需将代码更改为
ostream& operator<<(ostream& flux, Perso& const A) {
// ^
A.O_Show(flux);
return flux;
}
关于c++ - 为什么必须使用引用来返回/传递 std::ostream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24048592/