2012-11-15 47 views
0

如何訪問存儲在列表中的對象。我不想使用載體或#include <list>。任何見解都會被讚賞。謝謝!以下是我的班級定義:如何訪問存儲在列表中的對象

class UnsortedType // UnsortedType.h 
{ 
    private:     
     int length, currentPos; 
     book info[MAX_ITEMS]; 


    public:    
     UnsortedType(); 
     bool IsFull() const ; 
     int LengthIs() const ; // returns length of list 
     void RetrieveItem(book& item, bool& found); 
     void InsertItem(book item); // adds objects to the list 
     void DeleteItem(book item); // deletes objects in the list 
     void ResetList(); // resets list to default length -1 
     void GetNextItem(book& item); // gets the next object in the list 

}; 

存儲在此列表中的是書籍對象。包含在這些對象中的標題,作者,價格等...我的問題是如何訪問每個對象,一旦它們存儲在列表中。我想能夠比較存儲在列表中的對象的屬性。例如每本書的價格。

//main 
#include <iostream> 
#include "Book.h" 
#include "textbook.h" 
#include "Name.h" 
#include "unsorted.h" 

using namespace std; 


int main() 
{ 


    book b1("The Exception to the Rulers", "Amy", "Goodman", "Hyperion", 342, "1-4013-0131", 21.95,'N'); 
    book b2("Who moved my cheese", "Spencer", "Johnson", "Red Tree", 95, "0-399-14446-3", 19.99, 'H'); 
    book b3("Hellbound Hearts", "Neil", "Gaiman", "Dark Harvest", 326, "978-1-4391-4090-1", 16.00, 'F'); 

    UnsortedType L1; // creating a list "L1" 

    L1.InsertItem(b1); // populating the list with the first book 
    L1.InsertItem(b2); // populating the list with the second book 
    L1.InsertItem(b3); // populating the list with the third book 


    return 0; 
} 

這些是在book.h

// book.h 
enum RelationType 
{ 
    LESS, EQUAL, GREATER 
}; 

class book 
{ 
private: 
    string title; 
    Name aurthor; 
    string publisher; 
    string ISBN; 
    int pages; 
    float price; 
    char code; 


public: 
    RelationType ComparedTo(book) const; 
    class negativeNumber{}; 
    void setTitle(string); 
    void setAurthor(string f, string l); 
    void setPublisher(string); 
    void setISBN(string); 
    void setPages(int); 
    void setPrice(float); 
    void setCode(char); 
    string getTitle(); 
    Name getAurthor(); 
    string getPublisher(); 
    string getISBN(); 
    int getPages(); 
    float getPrice(); 
    char getCode(); 
    void PrintBook(); 
    book(); //default constructor 
    book(string, string, string, string, int, string, float, char); //constructor with args 
    ~book(); //Destructor 
}; 
+2

你寫的'UnsortedType'?因爲它看起來不可能使用。看起來有一個功能可以讓書出來,但是你不能指定哪一個。 DeleteItem apppears需要搜索。它使左右書籍的額外副本......看起來你可以遍歷列表,但只能有一次。 –

+0

基本上你有一個數組的包裝,所以你的問題是'我如何使用一個對象數組' - 回答'使用正常數組的東西'。例如,您可以添加一個GetItem(int idx)方法來獲取數組中的元素 – pm100

+0

如果想讓列表正常工作,爲什麼要使用如此複雜的'book'類? – Beta

回答

1
Book& UnsortedType::operator[](const int i)) 
{ 
    //check if array is in bounds 
    return info[i]; 
} 

的功能,這將允許您使用所述未分類的類L1訪問書的對象[3];

或在您的情況下,繼承人訪問每個對象,並確定本本的最低價格的例子:

double UnsortedType::FindLow() 
{ 
    double low = 9999;  //used to store lowest value 
    for(int i = 0; i < length; i++) //loop through array 
    { 
     if(info[i].price < low) //if the price of this book is lower than the current lowest 
      low = info[i].price; 
       //set low to new low 
    } 
    return low; 
} 
+0

所有書籍都存儲在L1中。但是,當我嘗試像L1 [i]那樣訪問列表(L1)中的第一個元素(第一本書)時,出現錯誤。我需要知道如何首先訪問列表中的對象。然後我可以開始操縱對象中的數據。 –

+0

你需要重載你的[]操作符或使用某個函數,否則請重新編輯我的帖子 –

+0

感謝!即時通訊不知道我真的明白爲什麼你必須超載運營商,但它的作品。 –

相關問題