[英]Solution for compiler warning: control reaches end of non-void function
I have the following code: 我有以下代码:
int cons_col()
{
for(int col =0; rx_state_== MAC_IDLE; col++)
return col;
}
It is like a counter that is supposed to return an integer when the condition rx_state_ == MAC_IDLE is met; 就像一个计数器,当满足条件rx_state_ == MAC_IDLE时,它应该返回一个整数; When I compile, I get the warning: control reaches end of non-void function.
当我编译时,我得到警告:控制到达非空函数的末尾。
Will this problem go away if add the following at the end of above function: 如果在上述功能的末尾添加以下内容,此问题会消失吗?
if (coll == 0)
return 0;
Thanks 谢谢
That code evaluates to this. 该代码对此进行评估。
int cons_col()
{
for( int col = 0; rx_state_ == MAC_IDLE; col++ )
{
return col;
// "return" prevents this loop from finishing its first pass,
// so "col++" (above) is NEVER called.
}
// What happens here? What int gets returned?
}
Note that this function will always immediately complete. 请注意,此功能将始终立即完成。
It does this: 它这样做:
col
to 0
. col
设置为0
。 rx_state_
is MAC_IDLE
. rx_state_
是否为MAC_IDLE
。 0
0
// What happens here?
// What happens here?
, and then reaches the end of the non-void function without returning anything. From your description, you probably wanted something like this. 根据您的描述,您可能想要这样的东西。
int cons_col()
{
int col = 0;
for( ; rx_state_ != MAC_IDLE; col++ )
{
// You may want some type of sleep() function here.
// Counting as fast as possible will keep a CPU very busy
}
return col;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.