当我尝试使用列表中的 push_back 方法时出现编译器错误。
这是我的代码:
// Point iterator to the proper warehouse.
set<cs3505::warehouse>::iterator curr_warehouse = warehouses.find(warehouse);
// Insert new inventory_item into the warehouse.
// Create a copy of today's date, and increment it.
cs3505::date exp_date = current_date;
exp_date.increment(curr_food.get_shelf_life());
// Create a new inventory item.
cs3505::inventory_item new_item(curr_food, exp_date);
// Set the quantity of the new item.
new_item.set_quantity(qty);
// Now insert the item.
// Adding new items being at the end ensures the oldest items will be at the
// beginning of the list.
(*curr_warehouse).inventory.push_back(new_item);
编译器错误:
report.cc:134:错误:将 'const std::list >' 作为 'void std::list<_Tp, _Alloc>::push_back(const _Tp&) 的 'this' 参数传递 [with _Tp = cs3505::inventory_item, _Alloc = std::allocator]' 丢弃限定符
我的代码的最后一行是第 134 行。感谢您的帮助。几个小时以来,我一直在努力解决这个问题。
这是inventory_item的定义:
/*
* An inventory item which includes a food item, an expiration date,
* and quantity.
*/
#include "inventory_item.h"
#include "date.h"
#include "food_item.h"
namespace cs3505
{
// inventory_item definitions
/*
* Constructs an inventory item.
*/
inventory_item::inventory_item(food_item &item, date &exp_date)
{
this->item = item;
this->expiration_date = exp_date;
this->quantity = 0;
}
/*
* Destructs a food item.
*/
inventory_item::~inventory_item() { }
/*
* Returns this inventory item's food item.
*/
food_item inventory_item::get_food_item()
{
return this->item;
}
/*
* Returns the expiration date for this inventory item.
*/
date inventory_item::get_exp_date()
{
return this->expiration_date;
}
/*
* Returns the quantity of this inventory item.
*/
int inventory_item::get_quantity()
{
return this->quantity;
}
/*
* Sets the quantity of this food item.
*/
void inventory_item::set_quantity(int change)
{
this->quantity = change;
}
}
我还有一个包含列表的自定义仓库类。我正在尝试将库存项目添加到该列表中。