库存构建

Inventory Building

本文关键字:构建      更新时间:2023-10-16

这是我将CD添加到库存的代码。我添加了它,但当我选择显示库存选项时,它不会显示在那里。

void addCD(Inventory i) {
int isbn = readIsbn();
if ( isbn ) {
    char buffer[BUF_SIZE];
    cout << "CD title: ";
    cin.getline(buffer,BUF_SIZE);
    string title(buffer);
    cout << "Developer name: ";
    cin.getline(buffer,BUF_SIZE);
    string developer(buffer);
    CD new_CD(isbn,title,developer);
    i.addItem(new_CD);
}

addItem的函数定义如下:

 Inventory::addItem(Item& new_item) {
// Lookup item in inventory
ItemTable::const_iterator i = _table.find(new_item.getIsbn());

if ( i == _table.end() ) {
    Item *ptrItem = new_item.clone();
    _table[ptrItem->getIsbn()] = ptrItem;
} else {
    cout << "Warning: Item with isbn " << new_item.getIsbn()
         << " already exists" << endl;
}
}

没有错误,只是库存没有显示新添加的项目。

对于函数void addCD(Inventory i),传递参数的副本,修改参数i不会影响调用方传递的内容。

您应该使用引用让函数修改调用者的局部变量。

尝试使用void addCD(Inventory &i)而不是void addCD(Inventory i)。(添加&