警告:控制到达非空函数的结尾(C++)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13216022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:05:53  来源:igfitidea点击:

warning: control reaches end of non-void function (c++)

c++controlsvoid

提问by Mesmer Lucas

i get this error and can't fix, i'm noob yet,if someone can help me i'll thank you this code come from xmplayer of libxenon (for jtag xbox)

我收到这个错误并且无法修复,我还是菜鸟,如果有人可以帮助我,我会感谢你这个代码来自 libxenon 的 xmplayer(对于 jtag xbox)

(i try search similar error, but i can't find what's wrong)

(我尝试搜索类似的错误,但我找不到问题所在)

  int FileSortCallback(const void *f1, const void *f2) {
    /* Special case for implicit directories */
    if (((BROWSERENTRY *) f1)->filename[0] == '.' || ((BROWSERENTRY *) f2)->filename[0] == '.') {
        if (strcmp(((BROWSERENTRY *) f1)->filename, ".") == 0) {
            return -1;
        }
        if (strcmp(((BROWSERENTRY *) f2)->filename, ".") == 0) {
            return 1;
        }
        if (strcmp(((BROWSERENTRY *) f1)->filename, "..") == 0) {
            return -1;
        }
        if (strcmp(((BROWSERENTRY *) f2)->filename, "..") == 0) {
            return 1;
        }
    }

    /* If one is a file and one is a directory the directory is first. */
    if (((BROWSERENTRY *) f1)->isdir && !(((BROWSERENTRY *) f2)->isdir)) return -1;
    if (!(((BROWSERENTRY *) f1)->isdir) && ((BROWSERENTRY *) f2)->isdir) return 1;

    //Ascending Name
    if (XMPlayerCfg.sort_order == 0) {
        return stricmp(((BROWSERENTRY *) f1)->filename, ((BROWSERENTRY *) f2)->filename);
    }
    //Descending Name
    else if (XMPlayerCfg.sort_order == 1) {
        return stricmp(((BROWSERENTRY *) f2)->filename, ((BROWSERENTRY *) f1)->filename);
    }
    //Date Ascending
    else if (XMPlayerCfg.sort_order == 2) {
        if ( ((BROWSERENTRY *) f2)->date == ((BROWSERENTRY *) f1)->date) { //if date is the same order by filename
            return stricmp(((BROWSERENTRY *) f2)->filename, ((BROWSERENTRY *) f1)->filename);
        } else {
            return ((BROWSERENTRY *) f1)->date - ((BROWSERENTRY *) f2)->date;
        }
    }
    //Date Descending
    else if (XMPlayerCfg.sort_order == 3) {
        if ( ((BROWSERENTRY *) f2)->date == ((BROWSERENTRY *) f1)->date) { //if date is the same order by filename
            return stricmp(((BROWSERENTRY *) f1)->filename, ((BROWSERENTRY *) f2)->filename);
        } else {
            return ((BROWSERENTRY *) f2)->date - ((BROWSERENTRY *) f1)->date;
        }
    }
}

回答by dasblinkenlight

The compiler analyzes your code, and sees that a return statement will be executed for all values of sort_orderbetween 0and 5, inclusive. However, if the sort_orderis negative or more than 5, the code would reach the end of the function without a return statement; that is why the compiler issues a warning.

编译器会分析您的代码,并发现将对sort_order介于0和之间的所有值执行 return 语句5,包括。但是,如果sort_order为负数或大于5,则代码将在没有返回语句的情况下到达函数的末尾;这就是编译器发出警告的原因。

Note that it may not be possible for sort_orderto be set to a negative number or a number over 5because of constraints in other parts of your code. However, the compiler does not know any of that, so it thinks that sort_ordercould have any value.

请注意,由于代码其他部分的限制,可能无法将sort_order其设置为负数或数字5。但是,编译器不知道任何这些,所以它认为这sort_order可能有任何价值。

To fix this problem, add an unconditional return statement at the end.

要解决此问题,请在末尾添加无条件 return 语句。