LeetCode报错解决 Char 5: error: non-void function does not return a value in all control paths

LeetCode报错解决 Char 5: error: non-void function does not return a value in all control paths [-Werror,-Wreturn-type]

在做LeetCode第268题"丢失的数字"的时候报了这个错误

原答案

LeetCode报错解决 Char 5: error: non-void function does not return a value in all control paths_第1张图片

Line 14: Char 5: error: non-void function does not return a value in all control paths [-Werror,-Wreturn-type]
    }
    ^
1 error generated.
//Char5:错误:非void函数未在所有控制路径中返回值[-Weror,-Rewesturn-type
答案修改为如下

LeetCode报错解决 Char 5: error: non-void function does not return a value in all control paths_第2张图片
正确。

分析:原答案使用IF语句时出错。原答案只有在if语句下才有返回值,尽管不会有其他情况,但严谨的编译器还是会报错,让你把其他情况都写清楚。

感谢这篇文章 http://t.csdnimg.cn/sX1L9

你可能感兴趣的:(LeetCode做题总结,leetcode,算法)