gpt4 book ai didi

c++ - 从函数返回字符串流

转载 作者:IT王子 更新时间:2023-10-28 23:27:57 26 4
gpt4 key购买 nike

显然我在这里遗漏了一些关于字符串流的重要内容,但有人可以解释原因

#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

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

最佳答案

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

关于c++ - 从函数返回字符串流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5119963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com