本文介绍了如何处理错误的“控制达到非空函数的结束” gcc警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  $ cat t.cpp 
int sign(int i){
if(i> 0)return 1;
if(i == 0)return 0;
if(i }
$ g ++ -c t.cpp -Wall
t.cpp:在函数'int sign(int)':
t.cpp:5:warning:非空函数的结束
$

我该怎么办?



停止使用-Wall,因为它显然是错误的?添加一个假回报0在结束?如果你不想添加else子句,因为它们会使得else子句的代码变得更加复杂。代码更长,那么也许你想删除最后的if并使代码更短:

  int sign i){
if(i> 0)return 1;
if(i == 0)return 0;
return -1; // i< 0
}

这不是一个更长的例子的简化:

  int sign(int i){
return(i> ; 0)? 1:((i }


$ cat t.cpp
int sign(int i) {
    if(i > 0) return 1;
    if(i == 0) return 0;
    if(i < 0) return -1;
}
$ g++ -c t.cpp -Wall
t.cpp: In function ‘int sign(int)’:
t.cpp:5: warning: control reaches end of non-void function
$

What do I do about this?

Stop using -Wall as it's clearly wrong? Add a bogus return 0 at the end? Clutter the code with "else" clauses?

解决方案

If you don't want to add "else" clauses because they would make the code longer, then perhaps you would like to remove the final "if" and make the code shorter:

int sign(int i) {
    if(i > 0) return 1;
    if(i == 0) return 0;
    return -1; // i<0
}

Or if you're really computing "sign" yourself and this isn't a simplification of some longer example:

int sign(int i) {
    return (i>0) ? 1 : ((i<0)?-1:0);
}

这篇关于如何处理错误的“控制达到非空函数的结束” gcc警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 06:08