简体   繁体   English

QT Creator,返回(C ++)

[英]QT Creator, return (C++)

Could somebody explain me why are these 2 types of return used? 有人可以解释一下为什么要使用这两种回报吗?

int parse(QTextStream& out, const QString fileName) {
    QDomDocument doc;
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly|QIODevice::Text)) {
        out<<"Datei "<<fileName<<" nicht lesbar"<<endl;


>   return 1;

    }
    QString errorStr;
    int errorLine;
    if (!doc.setContent(&file, false, &errorStr, &errorLine)) {
        out<<"Fehler in Zeile "<<errorLine<<": "<<errorStr<<endl;


>  return 2;

    }
    ...
}

Here stays a part of another program.Why the code here doesn`t work the same way with 这仍然是另一个程序的一部分。为什么这里的代码与

return 0; 返回0;

?

int main(int argc, char *argv[])
{
    QTextStream out(stdout);
       out.setCodec("UTF-8");
       if (argc != 3) {
           out<<"Usage: "<<argv[0]<<"   XML-Datei ist nicht vorhanden."<<endl;
           return(1);
       }
       List wayList(out, argv[1]);
       out<<"DOM-Baum gelesen."<<endl;
       wayList.convert(argv[2]);

return 1;

}

In your first example, the function returns early to indicate an error . 在第一个示例中,该函数提早返回以指示error The file couldn't be opened so the function returns a value to the caller of that function. 无法打开该文件,因此该函数向该函数的调用者返回一个值。 Couldn't set content, function returns a different value to the caller. 无法设置内容,该函数向调用者返回了一个不同的值。

if (!file.open(QIODevice::ReadOnly|QIODevice::Text)) {
    out<<"Datei "<<fileName<<" nicht lesbar"<<endl;

    return 1; // return value to caller
}

A function could for example call parse and check it's return value for success: 例如,一个函数可以调用parse并检查其返回值是否成功:

if ((parse(args...)) == 0) // success

In the end of function main() , return 0; 在函数main()的末尾, return 0; indicates that the program ran successfully. 表示程序已成功运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM