c++ - std::ostream to QString? -
is there way convert std::ostream qstring?
allow me expand in case useful: writing program in c++/qt , used (not) deal / debug exceptions using std::cout, in example:
std::cout << "error in void cat::eat(const bird &bird): bird has negative weight" << std::endl;
now want throw errors qstrings , catch them later, write instead:
throw(qstring("error in void cat::eat(const bird &bird): bird has negative weight"));
my issue i've been overloading operator << can use many objects, instance bird
, have written:
std::cout << "error in void cat::eat(const bird &bird): bird " << bird << " has negative weight" << std::endl;
is there way can throw qstring now? able write like:
std::ostream out; out << "error in void cat::eat(const bird &bird): bird " << bird << " has negative weight" << std::endl; throw(qstring(out));
but doesn't work. should do?
you can use std::stringstream
follows:
std::stringstream out; // ^^^^^^ out << "error in void cat::eat(const bird &bird): bird " << bird << " has negative weight" << std::endl; throw(qstring::fromstdstring(out.str())); // ^^^^^^^^^^^^^^^^^^^^^^^^^^
specifically, std::stringstream::str
member function std::string
, can pass qstring::fromstdstring
static member function create qstring
.
Comments
Post a Comment