Kto To Asked:2020-04-24 18:57:58 +0000 UTC2020-04-24 18:57:58 +0000 UTC 2020-04-24 18:57:58 +0000 UTC 为什么使用 QString arg 时程序可能会崩溃? 772 随着程序的长期使用,有时程序会在函数中崩溃QString::arg。有什么问题? 一些代码 QFile *file = new QFile("log.txt"); QTextStream out; out.setDevice(file); //********** 在发生错误的函数中,也将上述内容写入文件。一切都被正确记录。此外,它是一个日志功能。她经常被叫。但有时它会崩溃。 有时会显示一条消息Critical error detected c0000374 c++ 1 个回答 Voted Best Answer Kto To 2020-04-25T19:46:50Z2020-04-25T19:46:50Z 原来是因为没有成功翻译成枚举 enum class EE { e1 = 1, e2, e3 }; QString someFunc(EE e) { switch(e) { case EE::e1: return "EE::e1"; case EE::e2: return "EE::e2"; case EE::e3: return "EE::e3"; } } QTextStream SomeClass::operator<<(QTextStream &out, const MyStruct &r) { int i = 0; out << someFunc(EE(i)) << endl; // тут неопределенное поведение! out << r.otherFields << endl; // которая вызывала ошибку, но ниже. }
原来是因为没有成功翻译成枚举