尝试在列表中push_back.C++
Trying to push_back on a list. C++
当我尝试使用列表中的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>::p ush_back(const _Tp&) [_Tp = cs3505::inventory_item, _Alloc = std::分配器]"的"this"参数传递丢弃限定符
我的代码的最后一行是第 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;
}
}
我还有一个带有列表的自定义仓库类。我正在尝试将库存项目添加到该列表中。
这里的错误是因为您忽略了const
限定符。这是因为集合返回的迭代器必须是 const。之所以存在此限制,是因为集合中的所有元素都必须是唯一的;通过迭代器更改集合中元素的值可能会破坏此协定。
我找不到确切的参考资料(SGI的std::set
参考资料没有提到这一点),所以我将链接到另一篇Stackoverflow帖子,解释这一点: C++ STL 集更新很乏味:我无法就地更改元素
编辑:找到了。
std::set
是一种简单关联容器,这意味着值与键相同。以下段落总结了这一点:
类型 X::迭代器和 X::const_iterator 的类型必须相同。也就是说,简单关联容器不提供可变迭代器。
这确实意味着我的第一段在技术上略有错误。这并不是要确保不会将集合的元素从其下方更改为相同的值,而只是设计使然。它实际上是基本关联容器概念上"键是不可变的"不变性的副作用。
尽管如此,我还是把它留在那里,不让它成为重大编辑。
- 推导 std::vector::back() 的返回类型
- vector.back() 和 vector[vector.size() - 1] 之间的区别?
- vector.push_back(vector.back()+1) 是未定义的行为吗?
- 线路抑制状态错误 C4703 可能未初始化的局部指针变量"back"已使用
- std::vector using back(), pop_back(), push_back(), 得到'double free or corruption'错误
- vector.back()和vector.end()有什么区别
- 列表大小为 1,但 front() 和 back() 不相等
- 返回C++中没有 back() 方法的容器的最后一个元素?
- 在非空 std::list 上使用 std::list.back<int>() 时"Segmentation Fault"
- 我们可以使用Back() - 值索引在其上执行向量插入
- 当我运行以下代码添加str.front() str.back时,它给了我200个,但为什么
- 链表回推操作中需要'back pointer'
- 为什么 std::string 的 back() 应该返回对字符的引用?
- VC++ 自动说明符假定矢量的引用限定符<bool>::back的引用限定符
- C: strtok value gives back null
- vector.back() 可以用来为向量的最后一个元素赋值吗?
- 在std::vector中实现back()
- 在push_front属性上有点混淆.intlist.push_front(2 * intlist.back())会做些
- 将对象转换为 char,然后"uncasting it back"
- std::vector back()的奇怪行为