views:

49

answers:

2

I have a wstring stream that I'm using as sort of a buffer in my class and it is used by a good portion of the methods in this class. However, when I try to do something like this:

#include <sstream>

class foo
{
  public:
    void methodA(int x, int y); // Uses mBufferStream
    void methodB(int x, int y); // Uses mBufferStream

  private:
    std::wstringstream mBufferStream;
};

I get the following error:

error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

This isn't my exact class obviously, but it is the same setup. Any thoughts as to what I may be doing wrong? I am using Microsoft Visual Studio 2005

[Edit] showing use in method body in .cpp file (as an example of it's use):

void foo::methodA(int x, int y)
{
  mBufferStream << "From " << x << " To " << y;
  externalfunction(mBufferStream.str());  // Prints to message service
  mBufferStream.str(L"");
}
A: 

Assuming the missing ; on the externalfunction line is a typo, I wasn't able to get your exact error message, but it looks like perhaps externalfunction expects a std::string as its parameter. In fact mBufferStream.str() provides a std::wstring which can't be implicitly converted.

Mark B
Yes, that was a typo, thanks for the catch. The external function is overloaded to take wstring and const wstrings. I only get the error when I have the wstring in the header. If I put it in the method, it works fine.
Fry
A: 

This is because the compiler is implicitly declaring a copy constructor for class foo. std::wstringstream is noncopyable, because it inherits from ios_base.

Change your class to this:

#include <sstream>

class foo
{
  public:
    void methodA(int x, int y); // Uses mBufferStream
    void methodB(int x, int y); // Uses mBufferStream

  private:
    std::wstringstream mBufferStream;
    foo(const foo&); //noncopyable
    void operator=(const foo&)
};

and the compiler should point you at the culprit.

Billy ONeal