2015-12-07 40 views
0

有點困惑,我找不到任何與我的問題有關的東西。我可能會以錯誤的方式提問。C++重載數組索引操作符用於對象分配

我有這樣的代碼:

#include <iostream> 
#include <string> 

class AssociativeArray { 
public: 
    AssociativeArray(){ 
     for (int i = 0; i < tableSize; i++) { 
      HashTable[i] = new item; 
      HashTable[i]->name = "empty"; 
      HashTable[i]->price = 0.00; 
      HashTable[i]->next = NULL; 
     } 
    } 
    int HashKey(std::string key) { 
     int hash = 0; 
     int index; 

     for (int i = 0; i < key.length(); i++) { 
      hash = hash + (int)key[i]; 
     } 
     index = hash % tableSize; 
     return index; 
    } 
    void addItem(std::string name, double price) { 
     int index = HashKey(name); 
     if (HashTable[index]->name == "empty") { 
      HashTable[index]->name = name; 
      HashTable[index]->price = price; 
     } 
     else { 
      item* ptr = HashTable[index]; 
      item* n = new item; 
      n->name = name; 
      n->price = price; 
      n->next = NULL; 

      while (ptr->next != NULL) { 
       ptr = ptr->next; 
      } 
      ptr->next = n; 
     } 
    } 
    double& findPrice(std::string name) { 
     int index = HashKey(name); 
     bool found = false; 

     item* ptr = HashTable[index]; 
     item* price = ptr; 

     while (ptr != NULL) { 
      if (ptr->name == name) { 
       found = true; 
       price = ptr; 
      } 
      ptr = ptr->next; 
     } 
     if (found == true) { 
      return price->price; 
     } 
     else { 
      addItem(name, 0.00); 

      return price->price; 
     } 
    } 
    double& operator[](std::string name) { 
     return findPrice(name); 
    } 

private: 
    static const int tableSize = 5; 
    struct item { 
     std::string name; 
     double price; 
     item* next; 
    }; 

    item* HashTable[tableSize]; 
}; 
int main() { 

    AssociativeArray prices; 

    prices.addItem("Socks", 10.96); 

    std::cout << prices["Socks"] << std::endl; 
    prices["Socks"] = 7.77; 
    std::cout << prices["Socks"] << std::endl; 

    prices["Toaster Oven"] = 19.95; 
    std::cout << prices["Toaster Oven"] << std::endl; //Print 0.00, doesn't update price! 
    prices["Toaster Oven"] = 19.95; //update the price!? 
    std::cout << prices["Toaster Oven"] << std::endl; 

    system("PAUSE"); 
    return 0; 
} 

基本上,我嘗試通過散列的數組。我認爲我錯誤地重載了[]運算符。出於某種原因,作業不允許更新項目。有任何想法嗎?任何幫助或只是在正確的方向推動將是有益的!

我現在的方式是,當調用operator []時未找到對象時,會將新對象寫入該項目的散列中。如下所示:

while (ptr != NULL) { 
     if (ptr->name == name) { 
      found = true; 
      price = ptr; 
     } 
     ptr = ptr->next; 
    } 
    if (found == true) { 
     return price->price; 
    } 
    else { 
     addItem(name, 0.00); 

     return price->price; 
    } 

但是,在創建對象之後,double值的賦值似乎並沒有啓動。

prices["Toaster Oven"] = 19.95; 
std::cout << prices["Toaster Oven"] << std::endl; //Prints 0.00 Doesn't work 

prices["Toaster Oven"] = 19.95; 
std::cout << prices["Toaster Oven"] << std::endl; //Prints 19.95 works 

我應該以不同的方式做到這一點嗎?有什麼建議麼。太感謝了。

回答

2

問題是在這裏:

addItem(name, 0.00); // you construct new price item 
return price->price; // but here you return ref to some other item. 

檢查上述評論。