CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Apr 2010
    Posts
    10

    wostringstream causing problems

    Hi,

    I have a class which works just fine if I create a single instance.
    However if I create a vector of my class the following error comes up relating to having 'std::wostringstream' as a member of this class.

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

    Now I use wostringstream for logging as it's easy to use & each instance will either write to console and/or a different log file.

    Is there a simple solution or relatively straight forward alternative?

    I really don't want to have to remove this and go back swprintf_s

    Any help is appreciated.

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: wostringstream causing problems

    C++ streams are not copyable ... the easiest solution would be to hold
    new'ed pointers (either as the elements to the vector, or the stream object
    itself). You will need to keep track of the new/deletes if you do not
    use smart pointers.

  3. #3
    Join Date
    Apr 2010
    Posts
    10

    Re: wostringstream causing problems

    Thanks Philip,

    Not sure how to go about this as all still quite new to me, however will do some research.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: wostringstream causing problems

    Quote Originally Posted by dojima View Post
    Thanks Philip,

    Not sure how to go about this as all still quite new to me, however will do some research.
    The research can be done with two lines of code:
    Code:
    #include <sstream>
    
    int main()
    {
       std::wostringstream s1;
       std::wostringstream s2 = s1;
    }
    This should give you an error, and is the reason why you get the error in your program. To store objects in a vector, the items must be copyable. Since std::wostringstream is not copyable, you get the error.

    So the bottom line is that you cannot store stream objects as members and have correct copy semantics defined for the class without extra coding (you would need to code an assignment operator, copy constructor, and destructor)

    Either:

    1) store a pointer to the stream and instantiate those pointers (which could be problematic since you will have multiple pointers pointing to the same stream object, and you have to keep track of which one is the original when it comes time to deleting the right one),

    or

    2) attempt to redesign your code so that stream objects need not be members of the class. Maybe the latter is what you should investigate, since it is more than likely you are just using the stream object for local or temporary usage, and need not make it a member of the class.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 20th, 2010 at 10:17 AM.

  5. #5
    Join Date
    Apr 2010
    Posts
    10

    Re: wostringstream causing problems

    Thanks Paul,

    I implemented the latter, feels like a hack adding local stream to each method however does the job for now.

    I had it as a member so all methods could log to it centrally and then perform a single dump to disk.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: wostringstream causing problems

    If it's just a matter of logging, then perhaps you could just make it a static member so it doesn't need to be copied. Of course, all objects of the class will be writing to the same stream, so that might not be ideal; your call.
    Last edited by Lindley; April 21st, 2010 at 06:25 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured