warning: control reaches end of non-void function
时间: 2024-06-04 07:12:08 浏览: 18
This warning message typically occurs when a function is declared to return a value, but there is no return statement in the function. This means that if the function is called and the condition that would trigger a return statement is not met, the function will not return any value. This can lead to unexpected behavior in the program.
To fix this warning, make sure that all possible paths through the function include a return statement that returns the appropriate value. If there are certain conditions under which the function should not return a value, consider changing the return type to void.
相关问题
void函数报错warning: control reaches end of non-void function
这个错误提示表明你的函数返回类型应该是非 void 类型,但是在函数体内没有返回语句。因此编译器无法确定该函数的返回值,会发出警告。
你需要在函数体内添加一个返回语句,确保函数返回一个值。如果你的函数确实不需要返回值,那么你需要将函数的返回类型修改为 void。
下面是一些示例代码,演示如何修复这个错误:
```
// 返回类型为 int,但是函数体内没有返回语句
int foo() {
// do something
}
// 修复方法一:在函数最后添加一个返回语句
int foo() {
// do something
return 0; // 或者其他你需要的返回值
}
// 修复方法二:将返回类型修改为 void
void bar() {
// do something
}
```
warning: control reaches end of non-void function [-wreturn-type]
这个错误信息表示,在程序执行到函数的末尾时,编译器发现这个函数没有返回值。这是因为在这个函数的定义中没有包含 return 语句,或者 return 语句可能存在语法错误。您需要在函数的末尾添加一个 return 语句并返回一个有效的值,或者更改函数的返回类型为 void。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)