更新数组时出现问题?

时间:2018-11-12 15:59:18

标签: c++

我正在尝试为我们要分配的基于文本的冒险游戏创建一个库存系统,但是我的物品数组遇到了麻烦。

当我尝试更改/更新数组中的项目时,它会“更新”,但是当我在病房后用命令循环它时,它却没有更新

编辑方法正确打印

会寻求一些帮助

void Inventory::edit(int slot, Item item)
{
    cout << "changing slot: " << slot << endl;
    cout << "old Id: " << this->items[slot].getId() << endl;
    this->items[slot] = item;
    cout << "new Id: " << this->items[slot].getId() << endl;
}

changing slot: 0
old Id: -1
new Id: 1

void Inventory::printInventory()
{
    cout << "Your inventory:" << endl;
    for (Item item : this->items) {
        cout << "id: " << item.getId() << endl;
    }
}

prints
Your inventory:
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1
id: -1

Inventory.h

#pragma once
#include "Item.h"

using namespace std;

class Inventory
{
private:
public:
    Inventory();
    ~Inventory();
    Item items[28];
    void clear();
    void edit(int slot, Item item);
    void add(Item item);
    void add(Item item, int slot);
    void remove(Item item);
    bool contains(string name);
    int getNextFreeSlot();
    int getSlot(int itemId);
    int freeSlots();
    void printInventory();
};

Item.h

#include <string>

using namespace std;

class Item
{
private:
    int id;
    int amount;
    bool stackable;
public:
    //Used for setting the amount of the item
    void setAmount(int amount);
    //Used for grabbing the amount of the item
    int getAmount();
    //Sets the item id
    void setId(int id);
    //Grabs the item id
    int getId();
    Item();
    ~Item();
    //Used for generating an item with just an id, will automaticlly set the amount to 1
    Item(int id);
    //Used for generating an iteem with an id and an set amount
    Item(int id, int amount);
    //Gets the name of an item
    string getName();
    //Gets if an item is stackable
    bool isStackable();
};

1 个答案:

答案 0 :(得分:1)

在Player类中,您必须按引用返回。

进行修改:

Player.cpp

lines = lines.apply(Window.configure()
        .<List<String>>into(FixedWindows
                .of(Duration.standardSeconds(10))
        )
        .triggering(AfterProcessingTime.pastFirstElementInPane().plusDelayOf(Duration.standardSeconds(10)))
        .withAllowedLateness(Duration.standardSeconds(0))
        .discardingFiredPanes()
);

Player.h

Inventory& Player::getInventory()
{
    return this->inventory;
}