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
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.
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.
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.
Bookmarks