gpt4 book ai didi

c++ - 编译器警告: control reaches end of non-void function的解决方法

转载 作者:搜寻专家 更新时间:2023-10-31 00:39:24 24 4
gpt4 key购买 nike

我有以下代码:

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

关于c++ - 编译器警告: control reaches end of non-void function的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16207145/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com