N
Nick Keighley
Hi,
I want to store the representation of a class (called Thing in
this post) in a string. The class provides no access to Thing's
internal components but does provide an operator<<(). Defined
like this:-
friend ostream & operator<<(ostream &stream,const Thing &right);
So I thought: I'll use stringstream
#include <string>
#include <sstream>
Thing thing(...); // initialised somehow
std:stringstream id;
id << string("Thing: ") << thing;
// then id.str() is the string I want.
Unfortunatly, this doesn't work. The compiler says there is no
operator<< which takes a right-hand operand of Thing. Is (as I
assumed) ostringstream derived from ostring? Even if it is, presumably
this isn't supposed to work. Is there a way to do this? If I have to
modify Thing I might just give it a get() method for its internal value
(it's an integer...).
--
Nick Keighley
"It startled him even more when just after he was awarded the
Galactic Institute's Prize for Extreme Cleverness he got lynched
by a rampaging mob of respectable physicists who had finally
realized that the one thing they really couldn't stand was a
smart-ass."
Douglas Adams, The Hitchhiker's Guide to the Galaxy (1979)
I want to store the representation of a class (called Thing in
this post) in a string. The class provides no access to Thing's
internal components but does provide an operator<<(). Defined
like this:-
friend ostream & operator<<(ostream &stream,const Thing &right);
So I thought: I'll use stringstream
#include <string>
#include <sstream>
Thing thing(...); // initialised somehow
std:stringstream id;
id << string("Thing: ") << thing;
// then id.str() is the string I want.
Unfortunatly, this doesn't work. The compiler says there is no
operator<< which takes a right-hand operand of Thing. Is (as I
assumed) ostringstream derived from ostring? Even if it is, presumably
this isn't supposed to work. Is there a way to do this? If I have to
modify Thing I might just give it a get() method for its internal value
(it's an integer...).
--
Nick Keighley
"It startled him even more when just after he was awarded the
Galactic Institute's Prize for Extreme Cleverness he got lynched
by a rampaging mob of respectable physicists who had finally
realized that the one thing they really couldn't stand was a
smart-ass."
Douglas Adams, The Hitchhiker's Guide to the Galaxy (1979)