繁体   English   中英

编译器警告的解决方案:控制到达非void函数的结尾

[英]Solution for compiler warning: control reaches end of non-void function

我有以下代码:

int cons_col()
{
for(int col =0; rx_state_== MAC_IDLE; col++)
return col;
}

就像一个计数器,当满足条件rx_state_ == MAC_IDLE时,它应该返回一个整数; 当我编译时,我得到警告:控制到达非空函数的末尾。

如果在上述功能的末尾添加以下内容,此问题会消失吗?

if (coll == 0)
return 0;

谢谢

该代码对此进行评估。

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?
}

请注意,此功能将始终立即完成。

它这样做:

  • 将整数col设置为0
  • 检查一次 rx_state_是否为MAC_IDLE
  • 如果是,则返回0
  • 如果不是,则转到// What happens here? ,然后在不返回任何内容的情况下到达非void函数的末尾。

根据您的描述,您可能想要这样的东西。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM