-
April 20th, 2010, 05:38 AM
#1
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.
-
April 20th, 2010, 05:44 AM
#2
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.
-
April 20th, 2010, 07:08 AM
#3
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.
-
April 20th, 2010, 10:15 AM
#4
Re: wostringstream causing problems
Originally Posted by dojima
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.
-
April 21st, 2010, 03:28 PM
#5
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.
-
April 21st, 2010, 06:23 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|