从函数返回stringstream

时间:2011-02-25 16:43:29

标签: c++ 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::char_traits<char>, std::allocator<char> >&)’:
/usr/include/c++/4.4/iosfwd:75: note: synthesized method ‘std::basic_ios<char, std::char_traits<char> >::basic_ios(const 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::char_traits<char>, std::allocator<char> >::basic_stringbuf(const std::basic_stringbuf<char, std::char_traits<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::char_traits<char>, std::allocator<char> >::basic_stringstream(const std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >&)’:
/usr/include/c++/4.4/iosfwd:75: note: synthesized method ‘std::basic_stringbuf<char, std::char_traits<char>, std::allocator<char> >::basic_stringbuf(const std::basic_stringbuf<char, std::char_traits<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::char_traits<char>, std::allocator<char> >::basic_stringstream(const std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >&)’ first required here 

如何从函数中正确返回字符串流? (编辑:为完整的代码段添加了标题并修复了拼写错误)

6 个答案:

答案 0 :(得分:19)

在更正返回类型中的type-o后(由Mahesh注释),您的代码将无法在C ++ 03中编译,因为stringstream不可复制。但是,如果您的编译器支持C ++ 0x,那么打开它会允许您的代码编译,因为stringstreamMoveConstructible

答案 1 :(得分:12)

您无法按值返回函数流,因为这意味着您必须复制流。 C ++流不可复制。

答案 2 :(得分:3)

虽然它在C ++ 03中不起作用,但它应该在C ++ 11中工作。但是,当前的编译器可能仍然存在问题(由于缺乏完整的C ++ 11兼容性),例如上面的代码不能在g ++ 4.6.1中编译

答案 3 :(得分:3)

旧问题,但我相信实现您想要的正确方法是使用stringstream::str方法,该方法返回一个字符串对象,其中包含流缓冲区中当前内容的副本。

以下是string str() const;的示例。

std::string foo() {
    stringstream ss;
    ss << "add whatever you want to the stream" << 12 << ' ' << 13.4;
    return ss.str();
}

int main() {
    std::cout << foo();
    return 0;
}

打印:

add whatever you want to the stream 12 13.4 

答案 4 :(得分:2)

在C ++ 03中,您必须通过非const引用将stringstream作为参数传递,或者只返回结果字符串(ss.str()),因为您无法复制流。

答案 5 :(得分:-2)

您必须包含sstream并拥有std::stringstream而不是stringstream