执行对象函数时,C ++程序崩溃

时间:2014-11-21 18:26:38

标签: c++

以下是代码的一部分:

class Inventory {
    public:
        void PrintInventory();
        void AddItemToInventory();
        void UpdateItemQtyInInventory();
        void RemoveItemFromInventory();
    private:
        vector<Item*> inventory;
};

void Inventory::PrintInventory() {
    unsigned int i = 0;
    if (inventory.size() == 0) {
        cout << "No items to print." << endl;
    } else {
        for (i=0; i < inventory.size(); ++i) {
            cout << i << " - ";
            inventory.at(i)->Print();
        }
    }
    return;
}

void Inventory::AddItemToInventory() {
    string usrInptName = "";
    string usrInptQntyStr = "";
    string usrInptPriceStr = "";
    string usrInptOptn = "default";
    istringstream inSS;
    int usrInptQnty = 0;
    int usrInptPrice = 0;
    string usrInptOther = "";
    bool valid = false;

    while (valid == false) {
        cout << "Enter (b)ook or (p)roduce: ";
        getline(cin, usrInptOptn);

        if (usrInptOptn.size() == 0) {
                continue;
        } else if (usrInptOptn.at(0) == 'p') {
            Produce* prdct;

            cout << "Enter name of new produce: ";
            getline(cin, usrInptName);

            cout << "Enter the price per item: $";
            getline(cin, usrInptPriceStr);
            inSS.str(usrInptPriceStr);
            inSS >> usrInptPrice;
            inSS.clear();

            cout << "Enter quantity: ";
            getline(cin, usrInptQntyStr);
            inSS.str(usrInptQntyStr);
            inSS >> usrInptQnty;
            inSS.clear();

            cout << "Enter expiration date: ";
            getline(cin, usrInptOther);

            prdct = new Produce;
            prdct->SetName(usrInptName);
            prdct->SetPrice(usrInptPrice);
            prdct->SetQuantity(usrInptQnty);
            prdct->SetExpiration(usrInptOther);

            inventory.push_back(prdct);
            valid = true;

        } else if (usrInptOptn.at(0) == 'b') {
            Book* prdct;

            cout << "Enter name of new book: ";
            getline(cin, usrInptName);

            cout << "Enter the price per item: $";
            getline(cin, usrInptPriceStr);
            inSS.str(usrInptPriceStr);
            inSS >> usrInptPrice;
            inSS.clear();

            cout << "Enter quantity: ";
            getline(cin, usrInptQntyStr);
            inSS.str(usrInptQntyStr);
            inSS >> usrInptQnty;
            inSS.clear();

            cout << "Enter Author: ";
            getline(cin, usrInptOther);

            prdct = new Book;
            prdct->SetName(usrInptName);
            prdct->SetPrice(usrInptPrice);
            prdct->SetQuantity(usrInptQnty);
            prdct->SetAuthor(usrInptOther);

            inventory.push_back(prdct);
            valid = true;
        } else {
            cout << "Invalid choice\n";
        }

    }

    return;
}

Inventory* inventory;

int main() {
    string usrInptOptn = "default";

    while (true) {
        // Get user choice
        cout << "\nEnter (p)rint, (a)dd, (u)pdate, (r)emove, or (q)uit: ";
        getline(cin, usrInptOptn);

        // Process user choice
        if (usrInptOptn.size() == 0) {
            continue;
        } else if (usrInptOptn.at(0) == 'p') {
            inventory->PrintInventory();
        } else if (usrInptOptn.at(0) == 'a') {
            inventory->AddItemToInventory();
        } else if (usrInptOptn.at(0) == 'u') {
            inventory->UpdateItemQtyInInventory();
        } else if (usrInptOptn.at(0) == 'r') {
            inventory->RemoveItemFromInventory();
        } else if (usrInptOptn.at(0) == 'q') {
            cout << "\nGood bye." << endl;
            break;
        }

    }

    return 0;
}

程序在运行函数时崩溃。我一点也不知道为什么。 AddItemtoIventory函数在程序崩溃之前完成整个程序,而另一个程序崩溃,而其他程序只是崩溃。

1 个答案:

答案 0 :(得分:-1)

这里不需要全局变量。只需在main

范围内创建一个清单对象
int main() {
    Inventory inv;
    // Rest of your code below

    return 0;
}

或者,您仍然可以使用指针并使用新的。

int main() {
    Inventory* inv = new Inventory;
    // Rest of your code below

    return 0;
}