我在removeItem
方法中遇到問題,因爲在調用它之後發生錯誤。 在這種方法中,我試圖在nullptr
的參數中設置sku
的數組成員並將其「刪除」。 我認爲這與均衡有關:if(sku == shoppingList[i]->getSKU())
。或者可能與const
有關。該數組有指向Product
類型的對象的指針。在方法C++中設置const的字符串是否相等?
這屬於CustomerOrder.cpp
CustomerOrder::CustomerOrder()
: shoppingList()
{
}
void CustomerOrder::removeItem(const string &sku)
{
for(int i =0; i< 20; i++)
{
if(sku == shoppingList[i]->getSKU())
{
shoppingList[i] = nullptr;
}
}
}
這屬於Product.h
private:
std::string sku;
這屬於Product.cpp
const string & Product::getSKU() const
{
return sku;
}
什麼是shoppingList的類型?爲什麼不使用擦除/刪除成語? – Borgleader
private: std :: array shoppingList; –
user3348712