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;
}

基本上,我试图通过散列来制作一个数组。我想我把[]运算符重载错了。由于某些原因,分配不允许更新项目。有什么想法吗?任何帮助或只是朝着正确的方向推动都会有所帮助!

我现在的方法是,当调用运算符[]时找不到对象时,会将一个新对象写入该项的哈希中。如下所示:

    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;
    }

但是,双值的赋值似乎直到对象生成后才开始生效。

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

我应该换一种方式吗?任何建议。非常感谢。

问题就在这里:

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

查看上面的评论。

最新更新