问题描述
我得到了一段代码:
int check(int d,char arr [9] [9] ,int rep_arr [45] [3]){
int p = findProb(d,arr,rep_arr,0);
if(isIdeal(d,arr)== 1){
print_array(d,arr);
返回1;
else if(isIdeal(d,arr)== 0&& p == 0){
printf(失败\\\
);
返回0;
else if(isIdeal(d,arr)== 0&& p!= 0){
#递归地..
}
其中 isIdeal(d,arr)
只能等于 0
或 1
和 p
可以等于 0
或其他整数。
然而,编译器给了我标题中给出的错误。
稍后,我添加了 return 0
在那段代码的末尾。
现在它可以工作,但并没有以功能的方式工作,因为函数返回的内容真的很重要。
另一个重要的事情是,当我添加一个 else
块以避免在该网站的另一个主题上指出故障,它从来没有输入 else
块。然而,它总是返回0
是否添加一个 else
块,通过这种方式,所有的可能性都会以 return
行。如果你确定可能的值,那么你可以用你的整个函数替换:
int foo(int case1,int case2){
if(case1 == 1){
return val;
}
return case2 == 0? val2:val3;
}
并同时保持您的警告。
如果您担心 case1
可能不是 1
或 0
,然后改变为:
int foo(int case1,int case2 ){
assert(case1 == 0 || case1 == 1);
if(case1 == 1){
return val;
}
return case2 == 0? val2:val3;
}
I got a piece of code such that:
int check(int d, char arr[9][9], int rep_arr[45][3]) {
int p = findProb(d, arr, rep_arr, 0) ;
if (isIdeal(d, arr) == 1) {
print_array(d,arr) ;
return 1 ;
}
else if(isIdeal(d,arr) == 0 && p == 0){
printf("Fail\n") ;
return 0 ;
}
else if (isIdeal(d, arr) == 0 && p != 0) {
#do something recursively..
}
where isIdeal(d, arr)
can only be equal to 0
or 1
and p
can be equal to 0
or another integer .However, the compiler gives me the error that given in the title.
Later I added return 0
at the end of that piece of code.
Now it works but didn't work in a functional manner because what the function returns is really important.
Another crucial thing is that when I add an else
block to avoid failures indicated at another topic on that site see more at link, it never entered that else
block.However it always return 0
whether I add an else
block or not, by which way all the possibilities end up with a return
line. How ?
If you are sure about the possible values, then you can replace your entire function with:
int foo(int case1, int case2) {
if (case1 == 1) {
return val;
}
return case2 == 0 ? val2 : val3;
}
and silence your warning at the same time.
If you're concerned case1
may possibly be something other than 1
or 0
, then just change to:
int foo(int case1, int case2) {
assert(case1 == 0 || case1 == 1);
if (case1 == 1) {
return val;
}
return case2 == 0 ? val2 : val3;
}
这篇关于警告:控制到达非空函数结束[-Wreturn-type]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!