2011-10-24 26 views
1

我正在開發一個有Airline類的程序。 Airline類包含一系列動態Flight對象。雙astrisk語法和幫助調用成員函數

在航空類我有(不能編輯或更改,這是給我的任務的頭文件):

//Airline.h  
class Airline { 
public: 
    Airline();    // default constructor 
    Airline(int capacity); // construct with capacity n 
    void cancel(int flightNum);  
    void add(Flight* flight); 
    Flight* find(int flightNum); 
    Flight* find(string dest); 

    int getSize(); 


private: 
    Flight **array;  // dynamic array of pointers to flights 
    int capacity;   // maximum number of flights 
    int size;    // actual number of flights 

    void resize(); 
}; 

//Flight.h 
    class Flight { 
public: 
    Flight(); 
    // Default constructor 

    Flight(int fnum, string destination);  
    void reserveWindow();  
    void reserveAisle(); 
    void reserveSeat(int row, char seat); 

    void setFlightNum(int fnum); 

    void setDestination(string dest); 

    int getFlightNum(); 

    string getDest(); 

protected: 
    int flightNumber; 
    bool available[20][4]; //only four seat types per row; only 20 rows 
    string destination; 
}; 

我想在課堂上啓發其中一種發現方法。

對於這一點,我有:

Flight* Airline::find(int flightNum){ 
    bool found = false; 
    for(int i = 0; i < size; i++){ 
     if(array[i].getflightNum() == flightNum){ 
      found = true; 
      return array[i]; 
     } 
    } 

    if(!found) 
     return 0; 
} 

// Return pointer to flight with given number if present, otherwise 
// return 0. 

但它說,我試圖調用getFlightNum()方法時,需要一個類類型。我真的不明白這個錯誤。我是不是正確地調用方法?什麼是正確的語法?

+2

試試'數組[我] - > getflightNum()' –

+0

您還可以從'航空公司除去'found'所有實例:: find',因爲當你找到你立即從函數返回的項目時。因此你也不需要'if(!found)',如果你到了代碼中的那一點,那麼你沒有找到該項目,並且你不需要檢查'return 0'。 – AusCBloke

回答

4

因爲你面對的指針,而不是實際的對象,試試這個:

if(array[i]->getflightNum() == flightNum){  // Notice I am using -> instead of . 
    found = true; 
    return array[i]; 
} 
+0

謝謝!這工作。我從來沒有用過 - >之前。 – OghmaOsiris