Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.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++ 获取Qt中的最后一个错误号,C++;_C++_Qt_Debugging - Fatal编程技术网

C++ 获取Qt中的最后一个错误号,C++;

C++ 获取Qt中的最后一个错误号,C++;,c++,qt,debugging,C++,Qt,Debugging,我正在测试一个函数,它应该创建并写入一个文件,该文件的名称作为函数的参数给出。函数在失败时返回false。有没有办法在windows上获得像GetLastError()这样的错误号 对于QFile类的方法,您可以使用返回枚举的方法如果您对获取文件操作的“最后一个”错误感兴趣,可以执行以下操作: bool makeSomethingWithFile(const QString &fileName, QString *error) { QFile file(fileName);

我正在测试一个函数,它应该创建并写入一个文件,该文件的名称作为函数的参数给出。函数在失败时返回false。有没有办法在windows上获得像GetLastError()这样的错误号

对于QFile类的方法,您可以使用返回枚举的方法

如果您对获取文件操作的“最后一个”错误感兴趣,可以执行以下操作:

bool makeSomethingWithFile(const QString &fileName, QString *error)
{
    QFile file(fileName);
    // Perform something with the file
    // ...

    // On error
    if (file.error() != QFile::NoError) {
        *error = file.errorString();
        return false;
    }
    return true;
}
调用此函数时,只需提供以下错误字符串:

QString error;
if (!makeSomethingWithFile("myfile", &error)) {
    qDebug() << "The error occurred:" << error;
}
QString错误;
如果(!makeSomethingWithFile(“myfile”、&错误)){

qDebug()哪个函数失败?应该创建并写入文件的函数,必须有某种方法找到最后一个错误??问题是QFile完全在函数内部处理,它只返回true或false,是否没有windows之类的一般错误代码?您需要全局性的错误代码?如果是,则定义一个全局变量更新了你的函数,但是这种方法有缺点。这是一个好主意。这不必是永久性的,只要我能找出哪里出了问题,我就能解决它,谢谢!!