加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 从函数返回stringstream

发布时间:2020-12-16 03:18:40 所属栏目:百科 来源:网络整理
导读:显然,我在这里总是丢失一些关于stringstream的重要内容,但有人可以解释为什么 #include sstreamusing namespace std;stringstream foo() { stringstream ss; return ss;} 失败了 In file included from /usr/include/c++/4.4/ios:39,from /usr/include/c++/4
显然,我在这里总是丢失一些关于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.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读