2012-10-01 46 views
-1

我創建一個應用程序來跟蹤書店庫存。它將把文件路徑轉換爲包含信息的文本文件作爲main的參數。我遇到的問題似乎是每當我嘗試訪問我用來存儲我的Item對象的類狀態向量清單的元素時。首先是類頭,然後是方法定義,然後是main。提前致謝。分段錯誤對象

#ifndef _BOOKSTORE_H_ 
    #define _BOOKSTORE_H_ 

    #include <vector> 
    #include <iostream> 
    #include <fstream> 
    #include <string> 
    #include <sstream> 
    #include <algorithm> 
    #include <iterator> 
    #include <stdlib.h> 

    #include "Item.h" 
    #include "Paperback.h" 
    #include "Hardcover.h" 
    #include "Audiobook.h" 
    #include "Ebook.h" 

    using namespace std; 

    class Bookstore { 

    //States 
    private: 
     vector<Item*> inventory; 
    //Behaviors 
    public: 
     Bookstore(); 
     void loadInventory(const char*); 
     void searchInventory(string); 
     unsigned int inventorySize(); 
     void printInventory(); 
     vector<string> split(string); 

    }; 

    #endif 


    #include "Bookstore.h" 

    Bookstore::Bookstore() {} 

    void Bookstore::loadInventory(const char* filepath) { 

     string ty = ""; 
     string ti = ""; 
     string au = ""; 
     string pri = ""; 
     string f = ""; 
     string cd = ""; 
     string pro = ""; 

     string line; 
     ifstream ifs; 
     ifs.open (filepath); 

     if (ifs.is_open()) { 
      while (ifs.good()) { 
       ty = ""; 
       ti = ""; 
       au = ""; 
       pri = ""; 
       f = ""; 
       cd = ""; 
       pro = ""; 

       getline(ifs, line); 

       if (line.compare("Paperback") == 0) { 
        ty = "Paperback"; 
        getline(ifs, line); 
        ti = line; 
        getline(ifs, line); 
        au = line; 
        getline(ifs, line); 
        pri = line; 
        Paperback p(ty, ti, au, pri); 
        inventory.push_back(&p); 
       } 

       if (line.compare("Hardcover") == 0) { 
        ty = "Hardcover"; 
        getline(ifs, line); 
        ti = line; 
        getline(ifs, line); 
        au = line; 
        getline(ifs, line); 
        pri = line; 
        getline(ifs, line); 
        f = line; 
        Hardcover h(ty, ti, au, pri, f); 
        inventory.push_back(&h); 
       } 

       if (line.compare("Audio") == 0) { 
        ty = "Audio"; 
        getline(ifs, line); 
        ti = line; 
        getline(ifs, line); 
        au = line; 
        getline(ifs, line); 
        pri = line; 
        getline(ifs, line); 
        cd = line; 
        Audiobook a(ty, ti, au, pri, cd); 
        inventory.push_back(&a); 
       } 

       if (line.compare("Electronic") == 0) { 
        ty = "Electronic"; 
        getline(ifs, line); 
        ti = line; 
        getline(ifs, line); 
        au = line; 
        getline(ifs, line); 
        pri = line; 
        getline(ifs, line); 
        pro = line; 
        Ebook e(ty, ti, au, pri, pro); 
        inventory.push_back(&e);    
       } 

      } 
     } 

     ifs.close(); 
     cout << inventory.size() << endl; 

     for (unsigned int i=0; i<inventory.size(); i++) { 
      inventory.at(i)->printItem(); 
     } 
    } 

    void Bookstore::searchInventory(string query) { 

     vector<string> searchStr; 
     int count; 
     string currentStr; 

      for (unsigned int i=0; i<inventory.size(); i++) { 

        currentStr = inventory.at(i)->getTitle(); 
        searchStr = split(currentStr); 

        for(unsigned int j = 0; j < searchStr.size(); j++) { 
         if (searchStr[j].compare(query)==0) { 
          inventory.at(i)->printItem(); 
          count++; 
         } 
        } 

        currentStr = inventory.at(i)->getAuthor(); 
        searchStr = split(currentStr); 

        for(unsigned int j = 0; j < searchStr.size(); j++) { 
         if (searchStr[j].compare(query)==0) { 
          inventory.at(i)->printItem(); 
          count++; 
         } 
        } 

        if ((i==(inventory.size()-1))&&(count==0)) { 
         cout << "Sorry, no matching results were found." << endl; 
        } else if ((i==(inventory.size()-1))&&(count!=0)) { 
         cout << "Query complete." << endl; 
        } 
      } 
    } 

    unsigned int Bookstore::inventorySize() { 
     unsigned int num = inventory.size(); 
     return num; 
    } 

    void Bookstore::printInventory() { 
     for (unsigned int i = 0; i < inventory.size(); i++) { 
      inventory.at(i)->printItem();  
     } 

    } 

    vector<string> Bookstore::split(string s) { 
     vector<string> pieces; 
     istringstream iss(s); 
     copy(istream_iterator<string>(iss), istream_iterator<string>(), 
      back_inserter<vector<string> >(pieces)); 

     return pieces; 
    } 

    #include "Bookstore.h" 

    int main(int argc, char** argv) { 

     Bookstore current; 

     if (argc==2) { 
      current.loadInventory(argv[1]); 
     } 

     cout << current.inventorySize() << endl; 

     //Initialize variables 
     char status = 'R'; 
     string input = ""; 
     int iter = 1; 

     //Run application until quit 
     while (status=='R') { 

      //Provide menu 
      if (iter==1) { 
       cout << "Welcome to Bookstore Inventory 9000." << endl; 
      } 

      if (current.inventorySize()==0) { 
        cout << "There are no entries!" << endl; 
        status = 'Q'; 
      } else { 

       cout << "Would you like to (V)iew all, (S)earch, or (Q)uit?" << endl; 

       getline(cin, input); 

       if (input.compare("V")==0) { 
        current.printInventory();  
       } else if (input.compare("S")==0) { 
        cout << endl; 
        cout << "What are you looking for?" << endl; 
        getline(cin, input); 
        current.searchInventory(input); 
        cout << endl; 
       } else if (input.compare("Q")==0) { 
        status = 'Q'; 
        cout << "Thank you for perusing our inventory. Goodbye." << endl; 
       } else { 
        cout << endl; 
        cout << "This is not a valid choice. Please try again." << endl; 
        cout << endl; 
       } 
      } 

      iter++; 

     } 

    return -1; 

    } 
+0

你應該減少你必須演示您遇到的問題最小量的代碼量。另外,我們需要知道失敗的原因。 – CrazyCasta

回答

1

您的問題不在at()。你的問題是,你存儲遠地址到一個局部變量,再後來你正在使用的指針,即使局部變量已經不存在了。

具體來說,在loadInventory,這裏有一個例子:

if (line.compare("Paperback") == 0) { 
       ty = "Paperback"; 
       getline(ifs, line); 
       ti = line; 
       getline(ifs, line); 
       au = line; 
       getline(ifs, line); 
       pri = line; 
       Paperback p(ty, ti, au, pri); 
       inventory.push_back(&p); 
      } 

注意在第二個到最後你如何創建一個名爲「P」本地(棧)變線。在最後一行中,您將該局部變量的地址存儲起來。一旦代碼碰到上述引用中的最後一個大括號,名爲「p」的變量就會消失,並且向量中的指針已不再適用。

一個可能的解決方案是動態分配:

Paperback* p = new Paperback(ty, ti, au, pri); 
inventory.push_back(p); 

請注意,如果你這樣做,你必須自行刪除這些指針一旦你inventory向量與他們所做的。

+0

所以顯然我太新了,甚至沒有上升,但我的上帝,先生,你剛剛過了我的夜晚。在午夜的最後期限5分鐘後,這正是我忽略的,唯一的錯誤與我的代碼。在一對夫婦的新聞和析構函數拋出因此,一切工作。我可以請你喝一杯嗎?認真。謝謝。 –

+0

很高興我能幫到你。 –