有人建议为什么我的代码在 basic_ios 和 sstream 中创建错误?
我收到以下错误,在花费数周时间(非常兼职)获取我的代码中的错误后,这是一个轻微的改进:
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\sstream(451) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> C:\Program Files\Microsoft Visual Studio 9.0\VC\include\ios(151) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ostringstream<_Elem,_Traits,_Alloc>::basic_ostringstream(const std::basic_ostringstream<_Elem,_Traits,_Alloc> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
我正在从 CLI 迁移到 std::
类,我对此的经验更少。
我认为它所说的“此诊断发生在编译器生成的函数中”所引用的函数是我的 std::ostringstream os;。
我使用 os
如下:
os << "Level: " << levelName << ", contains unexpected header at line " << (numMatched + 1)
<< "\nExpected:\n" << longStringHeader;
addToErrorSTDstring(os);
因为 std::string
连接每次添加都会占用一行。
我使用 ostringstream 的唯一方法是
bool LevelParser::addToErrorSTDstring(std::ostringstream os){
我相信 ildjarn 敏锐地发现了我的问题。
I get the following errors which is a slight improvement after taking weeks (very part-time) getting the errors out of my code:
1>C:\Program Files\Microsoft Visual Studio 9.0\VC\include\sstream(451) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> C:\Program Files\Microsoft Visual Studio 9.0\VC\include\ios(151) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1> This diagnostic occurred in the compiler generated function 'std::basic_ostringstream<_Elem,_Traits,_Alloc>::basic_ostringstream(const std::basic_ostringstream<_Elem,_Traits,_Alloc> &)'
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>,
1> _Alloc=std::allocator<char>
1> ]
I am migrating from the CLI to std::
classes which I have even less experience with.
I think where it is saying "This diagnostic occurred in the compiler generated function" the function being referred to is my std::ostringstream os;
.
I use os
like as follows:
os << "Level: " << levelName << ", contains unexpected header at line " << (numMatched + 1)
<< "\nExpected:\n" << longStringHeader;
addToErrorSTDstring(os);
because std::string
concatenation took a line up with each addition.
The only way I use ostringstream
is
bool LevelParser::addToErrorSTDstring(std::ostringstream os){
which I believe ildjarn just perspicaciously identified as the problem for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该错误表明您正在尝试复制
std::ostringstream
的实例,但所有标准流都是不可复制对象(在 C++11 中它们是,但是,可移动)。这是问题的根源
,但是在没有看到您的实际代码的情况下,我们无法为您提供有关如何修复它的具体建议。编辑(响应OP的编辑):
addToErrorSTDstring
无疑按值采用std::ostringstream
,即具有如下签名:相反,您需要通过引用传递它,即将签名更改为:
或:(
取决于您如何使用它)。
如果您是 C++ 新手,那么您需要停止正在做的事情并温习语言基础知识,例如引用和常量正确性。
The error indicates that you're trying to copy an instance of
std::ostringstream
, but all standard streams are non-copyable objects (in C++11 they are, however, movable).That's the root of the problem
, but without seeing your actual code we can't give you concrete suggestions on how to fix it.EDIT (in response to OP's edit):
addToErrorSTDstring
undoubtedly takes astd::ostringstream
by value, i.e. has a signature like:Instead, you need to pass it by reference, i.e. change the signature to something like:
or:
(depending on how you use it).
If you're new to C++, then you need to stop what you're working on and brush up on language fundamentals such as references and const-correctness.