代码如下:
inline ostream operator<<(ostream& os, const istring s)
{
return os<<string(s.c_str(), s.length());
}
编译它出现:std::ios_base::ios_base(const std::ios_base&)' is private 错误。
这是因为流作为参数传递时必须通过引用传递,所以正确的代码是
inline ostream& operator<<(ostream& os, const istring s)
{
return os<<string(s.c_str(), s.length());
}
用std::ifstream,std::ofstream作为函数参数传递时,必须通过引用传递,因为其copy方法被私有化,从而保证对象的唯一性。
正确代码
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
错误代码