c – 从函数返回stringstream

前端之家收集整理的这篇文章主要介绍了c – 从函数返回stringstream前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
显然,我在这里总是丢失一些关于stringstream的重要内容,但有人可以解释为什么
#include <sstream>
using namespace std;

stringstream foo() {
  stringstream ss;
  return ss;
}

失败了

In file included from /usr/include/c++/4.4/ios:39,from /usr/include/c++/4.4/ostream:40,from /usr/include/c++/4.4/iostream:40,from rwalk.cpp:1:/usr/include/c++/4.4/bits/ios_base.h: In copy constructor ‘std::basic_ios<char,std::char_traits<char> >::basic_ios(const std::basic_ios<char,std::char_traits<char> >&)’:/usr/include/c++/4.4/bits/ios_base.h:790: error: ‘std::ios_base::ios_base(const std::ios_base&)’ is private
/usr/include/c++/4.4/iosfwd:47: error: within this context
/usr/include/c++/4.4/iosfwd: In copy constructor ‘std::basic_stringstream<char,std::char_traits<char>,std::allocator<char> >::basic_stringstream(const std::basic_stringstream<char,std::allocator<char> >&)’:
/usr/include/c++/4.4/iosfwd:75: note: synthesized method ‘std::basic_ios<char,std::char_traits<char> >&)’ first required here 
/usr/include/c++/4.4/streambuf: In copy constructor ‘std::basic_stringbuf<char,std::allocator<char> >::basic_stringbuf(const std::basic_stringbuf<char,std::allocator<char> >&)’:
/usr/include/c++/4.4/streambuf:770: error: ‘std::basic_streambuf<_CharT,_Traits>::basic_streambuf(const std::basic_streambuf<_CharT,_Traits>&) [with _CharT = char,_Traits = std::char_traits<char>]’ is private
/usr/include/c++/4.4/iosfwd:63: error: within this context
/usr/include/c++/4.4/iosfwd: In copy constructor ‘std::basic_stringstream<char,std::allocator<char> >&)’:
/usr/include/c++/4.4/iosfwd:75: note: synthesized method ‘std::basic_stringbuf<char,std::allocator<char> >&)’ first required here 
rwalk.cpp: In function ‘std::stringstream foo()’:
rwalk.cpp:12: note: synthesized method ‘std::basic_stringstream<char,std::allocator<char> >&)’ first required here

一个函数如何正确地返回一个字符串流? (编辑:添加了一个完整的代码片段和固定的打字错误标题)

解决方法

在返回类型(由Mahesh注释)中更正了类型o之后,您的代码将不会在C 03中编译,因为stringstream不可复制.但是,如果您的编译器支持C 0x,那么打开它就可以编译代码,因为stringstream是MoveConstructible.

猜你在找的C&C++相关文章