2013-04-25 202 views
2

我有以下代碼:解決方案編譯器警告:控制到達非void函數結束

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

它就像一個應該返回一個整數,當條件rx_state_ == MAC_IDLE滿足計數器; 當我編譯時,我得到警告:控制達到非void函數的結束。

請問這個問題消失,如果添加以下在上面的函數的末尾:

if (coll == 0) 
return 0; 

感謝

+0

你的功能,你想讓它工作,只是返回0 – 2013-04-25 05:43:33

回答

5

該代碼計算了這一點。

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

注意,此函數始終立即完整。

它這樣做是:

  • 設置整col0
  • 支票一次如果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; 
} 
+0

'檢查是否0也是你MAC_IDLE.'can解釋這一點嗎? – 2013-04-25 05:47:47

+0

@Koushik Typo就我而言! :-) – 2013-04-25 05:52:51

+0

啊謝謝。 +1 :-) – 2013-04-25 05:54:29

相關問題