2011-09-16 24 views
0

我不斷收到以下錯誤,我不知道什麼是錯四處錯誤:控制到達非void函數的結束不知道爲什麼

cc1plus: warnings being treated as errors 
scene.cpp: In member function ‘Image* Scene::getpicture(int) const’: 
scene.cpp:179: error: control reaches end of non-void function 

這裏是一個錯誤是在代碼的一部分:

Image* Scene::getpicture(int index) const { 

    if(index<0 || index >maximum) 
     cout << "invalid index" << endl; 
    else { 
     return images[index]; 
    } 
} 
+0

是'圖像[]'圖像'或'*圖像'數組的數組? –

回答

4

如果代碼沒有輸入else語句,則不會返回任何結果。因此,您需要在最後或輸入if語句時插入返回值。

3

if語句的條件爲真時,函數不返回值,因爲在這種情況下沒有執行return語句。

您需要返回一個值(或拋出異常)。例如:

Image* Scene::getpicture(int index) const { 
    if (index < 0 || index > maximum) { 
     cout << "invalid index" << endl; 
     return NULL; // Return NULL in case of an invalid index 
    } else { 
     return images[index]; 
    } 
} 
相關問題