Stringstream to const char* issue

I'm sure that this is just a stupid problem because I've done this a hundred times, but I can't find the problem.

On line 25 below, I am inducing a complilation error and I can't figure out why.
It has something to do with std::string stringstream::str() usage.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class MatrixException : public std::exception
{
private:
  std::stringstream error;

public:
  MatrixException(MException e, std::string file = "", int line = -1)
  {
    switch (e)
    {
    case invAccess:
      error << "Access to matrix data was out of bounds"; break;
    case invSet:
      error << "Attempt to set matrix data was out of bounds"; break;
    case wrongSize:
      error << "Matrix operation was attempted with mismatched sizes"; break;
    }

    if (file.size()) error << " file: " << file; // Allows the user to use __FILE__
    if (line != -1 ) error << " line: " << line; // Allows the user to use __LINE__
  }

  virtual const char* what() const throw()
  {
    return error.str().c_str(); // Error here:
  }
};
1>C:\Program Files\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\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>    ]
Last edited on
Got it.

The problem wasn't in line 25, the problem was that I couldn't create a std::stringstream object as a member of MatrixException. I don't know why.

My work-around was to use a std::string member, and use std::stringstream locally in the constructor.
std::stringstream has a move constructor/operator in C++11, but it's not copiable? The plot thickens...

Edit: minor mistakes in expressing myself.
Last edited on
std::stringstream has a move constructor/operator in C++11, but it's not copiable?


Just like every other stream type. It doesn't make sense to copy a stream.
Of course. I'm just saying,
std::basic_stringstream<_Elem,_Traits,_Alloc>::basic_stringstream(const std::basic_stringstream<_Elem,_Traits,_Alloc> &)

looks a lot like a copy constructor...
.. because it is the
compiler generated
copy constructor which couldn't be made to work by said compiler.

It's essentially the same situation as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class A
{
public:
    A() {}
private:
    A(const A&);  // disable copy construction 
};

class B : public A
{
};

int main()
{
    B b ;
    B c(b) ;
}


If "=delete" was supported, perhaps the diagnostic could be more informative.

Topic archived. No new replies allowed.