簡體   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