【发布时间】:2018-02-12 23:24:54
【问题描述】:
创建 Inventory 类以及 item 类...处理库存“移除”库存中的项目。
#include "Inventory.h"
#include <iostream>
void Inventory::PrintInventory() {
for (auto i = items.begin(); i != items.end(); i++) {
std::cout << i->name << " " << i->numItem << std::endl;
}
}
void Inventory::ReceiveItem(Item item) {
items.push_back(item);
}
Item Inventory::TakeItem(int num) {
items.erase(items.begin() + num);
auto item = std::find(items.begin(), items.end(), num);
//issue is here
//my teacher said to set it up as Item Inventory that takes in an int.
return item;
}
//这是在actor类中...
void Actor::GiveItem(Actor* actor, Item item) {
int num = item.numItem;
actor->inventory.ReceiveItem(item);
inventory.TakeItem(num);
}
问题是......我不确定在 Inventory 类的 Item Inventory 函数中做什么,它应该返回一些东西,但我不知道为什么......老师无法访问。如果它应该返回一个 Item 对象,我需要从整数值中获取 Item 对象。 Item 对象类有一个 char* 名称;和 int numItem;
【问题讨论】:
-
我在这里没有看到任何实际问题。
-
澄清了这个问题。
-
我的猜测是 Item 是索引函数(类似于 [] 运算符),它返回指定为 0 或 1 的插槽中的项目,具体取决于您的规范。
-
您的rubber duck 想知道您希望如何在列表中删除后找到并返回该项目。
-
如果有更好的写法,请大哥帮忙。我同意返回一个将要删除的值是愚蠢的。这就是为什么我对退货部分也感到困惑......