Stringstream错误:无法访问在'std :: basic_ios< _Elem,_Traits> [英] Stringstream error: cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

查看:432
本文介绍了Stringstream错误:无法访问在'std :: basic_ios< _Elem,_Traits>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建一个简单的异常类扩展(我可以更容易地构造错误消息),我已隔离一个错误到以下简单的代码:

  #include< sstream> 
#include< string>
class myCout {
public:
std :: stringstream ssOut; //删除这会消除错误
template< typename T> myCout&运算符<< (const T& x){
//做一些格式化
return * this;
}
};

class myErr:public myCout {
public:
using myCout :: operator<< ;;
};

int main(int argc,const char * argv []){
throw myErr()< ErrorMsg< 1;
myCout()<< 消息将格式化;
return 0;
}

在编译时会产生此错误:

  1> C:\Program Files(x86)\ Microsoft Visual Studio 10.0 \VC\include\sstream(724):error C2248: 'std :: basic_ios< _Elem,_Traits> :: basic_ios':无法访问在'std :: basic_ios< _Elem,_Traits>'中声明的私有成员
1> with
1> [
1> _Elem = char,
1> _Traits = std :: char_traits< char>
1> ]
1> C:\Program Files(x86)\Microsoft Visual Studio 10.0 \VC\include\ios(176):请参见声明std :: basic_ios< _Elem,_Traits> :: basic_ios'
1> ; with
1> [
1> _Elem = char,
1> _Traits = std :: char_traits< char>
1> ]
1>该诊断发生在编译器生成的函数'std :: basic_stringstream< _Elem,_Traits,_Alloc> :: basic_stringstream(const std :: basic_stringstream< _Elem,_Traits,_Alloc>&)'
1& with
1> [
1> _Elem = char,
1> _Traits = std :: char_traits< char>,
1> _Alloc = std :: allocator< char>
1> ]

(实际上它更复杂,扩展了 std ::我已经看到



< member-declared-in-class-stdbasic-ios-elem-traits>以前的答案
,说明问题是由于没有通过引用传递流,但我不能看到我不是。 >

注释掉 std :: stringstream ssOut 可修复问题。为什么,如何解决根本问题?

解决方案

。但是,它意味着异常被复制为 throw 语句的一部分,因此它必须有一个可访问的复制构造函数。因为你有一个不可复制的成员( std :: stringstream ),你必须提供自己的复制ctor。


In creating a simple exception class extension (where I can construct error messages more easily), I've isolated an error down to the following simple code:

#include <sstream>
#include <string>
class myCout {
public:
    std::stringstream ssOut;    // Removing this gets rid of error
    template <typename T> myCout& operator << (const T &x) {
        // Do some formatting
        return *this;
    }
};

class myErr : public myCout {
public:
    using myCout::operator<<;
};

int main(int argc, const char* argv[]) {
    throw myErr() << "ErrorMsg" << 1;
    myCout() << "Message Will Be Formatted";
    return 0;
}

Which, on compiling, produces this error:

1>C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\sstream(724): 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 (x86)\Microsoft Visual Studio 10.0\VC\include\ios(176) : 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_stringstream<_Elem,_Traits,_Alloc>::basic_stringstream(const std::basic_stringstream<_Elem,_Traits,_Alloc> &)'
1>          with
1>          [
1>              _Elem=char,
1>              _Traits=std::char_traits<char>,
1>              _Alloc=std::allocator<char>
1>          ]

(In actual fact it's more complex and extends stuff like std::runtime_error)

I have seen previous answers which state the problem arises from not passing streams by reference, but I can't see how I'm not.

Commenting out the std::stringstream ssOut fixes the issue. Why, and how do I fix the underlying problem?

解决方案

You're throwing the exception by value, which is indeed recommended practice. However, it means the exception gets copied as part of the throw statement, so it must have an accessible copy constructor. And because you have a non-copyable member (std::stringstream), you must provide your own copy ctor.

这篇关于Stringstream错误:无法访问在'std :: basic_ios&lt; _Elem,_Traits&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆