Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ std::ostream到QString?_C++_Qt_Exception - Fatal编程技术网

C++ std::ostream到QString?

C++ std::ostream到QString?,c++,qt,exception,C++,Qt,Exception,有没有办法将std::ostream转换为QString 如果有用,请允许我展开: 我正在用C++/Qt编写一个程序,我过去(不是)只使用std::cout来处理/调试异常,例如: std::cout << "Error in void Cat::eat(const Bird &bird): bird has negative weight" << std::endl; 我的问题是,我一直在重载运算符您可以使用如下方法: std::stringstream ou

有没有办法将std::ostream转换为QString

如果有用,请允许我展开: 我正在用C++/Qt编写一个程序,我过去(不是)只使用std::cout来处理/调试异常,例如:

std::cout << "Error in void Cat::eat(const Bird &bird): bird has negative weight" << std::endl;
我的问题是,我一直在重载运算符您可以使用如下方法:

std::stringstream out;
//   ^^^^^^
out << "Error in void Cat::eat(const Bird &bird): bird " << bird << " has negative weight" << std::endl;
throw(QString::fromStdString(out.str()));
//           ^^^^^^^^^^^^^^^^^^^^^^^^^^
std::stringstream out;
//   ^^^^^^

outstd::stringstream
类可以接收重载的输入
尝试从字符串部分创建一个
std::stringstream
,然后在
QString
的构造函数中使用
ss.str()
?即
throw(QString(ss.str())
应该是
QString::fromStdString(ss.str())
-抱歉
std::cout << "Error in void Cat::eat(const Bird &bird): bird " << bird << " has negative weight" << std::endl;
std::ostream out;
out << "Error in void Cat::eat(const Bird &bird): bird " << bird << " has negative weight" << std::endl;
throw(QString(out));
std::stringstream out;
//   ^^^^^^
out << "Error in void Cat::eat(const Bird &bird): bird " << bird << " has negative weight" << std::endl;
throw(QString::fromStdString(out.str()));
//           ^^^^^^^^^^^^^^^^^^^^^^^^^^
#include <sstream>
#include <QtCore/QString>

int main() {
    int value=2;
    std::stringstream myError;
    myError << "Here is the beginning error text, and a Bird: " << value;
    throw(QString::fromStdString(myError.str()));
}