简体   繁体   English

在C ++向量中修改对象值时遇到问题

[英]Trouble with modifying object values in a c++ vector

I am trying to develop an inventory system for a game in c++. 我正在尝试为c ++中的游戏开发库存系统。 I have 2 header files one is named item and the other inventory these are sent into my main method. 我有2个头文件,其中一个被命名为item,另一个库存被发送到我的main方法中。 The way I have my code set up is when I send an Item object to my inventory the inventory checks to see if an object with the same ID exists. 设置代码的方式是,当我将Item对象发送到库存时,库存检查以查看是否存在具有相同ID的对象。 If it does then get the quantity Item value and increment it by one. 如果是,则获取数量Item值并将其增加1。 The problem is the change to the Quantity value does not save to the main Inventory object. 问题是对“数量”值所做的更改未保存到主“库存”对象。 Any and all help would be very appreciated. 任何和所有帮助将不胜感激。 Also sorry if my code is bad! 如果我的代码不正确,也请您谅解! I am not going to include all the code, and I am sure that all the proper #include functions are in! 我不会包含所有代码,而且我肯定所有正确的#include函数都已包含在内!

The real issue seems to be somewhere in the actual new Quantity value not being added to the main Inventory object. 真正的问题似乎在实际的新“数量”值中没有添加到主“库存”对象中。

#include "stdafx.h"
#include <iostream>
#include "Windows.h"
#include "Item.h"
#include "Inventory.h"
#include <ctime>
#include <fstream>
#include <conio.h>
#include <stdio.h>
#include <windows.h>
#include <string>
#include <cstdio>
#include <vector>







// Functions!
void gotoxy(int x, int y)
{
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void setColor(int color) // 0 Black, 1 Blue, 2 Green, 3 Light Blue, 4Red 5     Purple 6 Yellow 7 White
{
    HANDLE  hConsole;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hConsole, color);

}
void clearColor()
{
    HANDLE  hConsole;
    hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    SetConsoleTextAttribute(hConsole, 7);

}
void ShowConsoleCursor(bool showFlag)
{
    HANDLE out = GetStdHandle(STD_OUTPUT_HANDLE);

    CONSOLE_CURSOR_INFO     cursorInfo;

    GetConsoleCursorInfo(out, &cursorInfo);
    cursorInfo.bVisible = showFlag; // set the cursor visibility
    SetConsoleCursorInfo(out, &cursorInfo);
}






//
// Menu SetUp!

void menuArea()
{
    gotoxy(40, 4);
    setColor(2);
    std::cout << "--------Menu--------" << std::endl;

    gotoxy(40, 12);
    std::cout << "--------------------" << std::endl;
    clearColor();

}

void gatherArea()
{
    gotoxy(45, 6);
    setColor(3);
    std::cout << "-Gather" << std::endl;
    clearColor();
}
void recoveryArea()
{
    gotoxy(45, 8);
    setColor(5);
    std::cout << "-Recovery" << std::endl;
    clearColor();
}
void inventoryArea()
{
    gotoxy(45, 10);
    setColor(4);
    std::cout << "-Inventory" << std::endl;
    clearColor();
}
//
// Menu Selector!
void gatherOption()
{
    gotoxy(40, 6);
    std::cout << "(G)" << std::endl;
}
void recoverOption()
{
    gotoxy(40, 8);
    std::cout << "(R)" << std::endl;
}
void inventoryOption()
{
    gotoxy(40, 10);
    std::cout << "(I)" << std::endl;
}
void gatherSelected()
{
    setColor(5);
    gotoxy(40, 6);
    std::cout << "(G)" << std::endl;
    clearColor();
}
void recoverSelected()
{
    setColor(5);
    gotoxy(40, 8);
    std::cout << "(R)" << std::endl;
    clearColor();
}
void inventorySelected()
{
    setColor(5);
    gotoxy(40, 10);
    std::cout << "(I)" << std::endl;
    clearColor();
}
//
// Menu Viewer!

void viewClear()
{
    gotoxy(0, 4);
    for (int i = 0; i < 40; i++)
    {
        std::cout << "\t\t\t\t\t" << std::endl;
    }



}

void viewGather()
{
    gotoxy(12, 4);
    std::cout << "-----Gather-----\n" << std::endl;
    std::cout << "\t 1) Gather Wood    (5 sec)\n" << std::endl;
    std::cout << "\t 2) Gather Berries (5 sec)\n" << std::endl;

}
void viewRecover()
{
    gotoxy(12, 4);
    std::cout << "-----Recover-----\n" << std::endl;
    std::cout << "\t 1) Eat Berries (5 sec)\n" << std::endl;
    std::cout << "\t 2) Sleep      (10  sec)\n" << std::endl;


}




//
// Figuring out the inventory

void viewInventory(Inventory inventory)
{
    std::string inv = inventory.display(inventory);
    gotoxy(12, 4);
    std::cout << inv << std::endl;
}


Inventory setInventory(Inventory inventory)
{
    Item woodItem(1, "Wood;");
    Item berriesItem(2, "Berries");

    inventory.addItem(woodItem);
    inventory.addItem(berriesItem);

    return inventory;
}

//
//Timer Stuff!
void timerSetup()
{
    setColor(6);
    gotoxy(5, 2);
    std::cout << "Client Up Time:" << std::endl;
    clearColor();

}

void getUpTime()
{
    gotoxy(22, 2);
    int seconds = (int)std::clock() / 1000;
    int minutes = (int)seconds / 60;
    seconds = seconds % 60;
    if (seconds < 10 && minutes < 10)
        std::cout << "0" << minutes << ":0" << seconds << std::endl;

    else if (seconds < 10)
        std::cout << minutes << ":0" << seconds << std::endl;

    else if (minutes<10)
        std::cout << "0" << minutes << ":" << seconds << std::endl;
    else
        std::cout << minutes << ":" << seconds << std::endl;
}

//
void drawMenu()
{
    menuArea();
    gatherArea();
    recoveryArea();
    inventoryArea();
    timerSetup();
    gatherOption();
    recoverOption();
    inventoryOption();

}





int main()
{

    Inventory inventory;
    drawMenu();
    std::clock_t start;
    inventory = setInventory(inventory);



    while (true)
    {
        ShowConsoleCursor(false);
        getUpTime();


        if (GetAsyncKeyState(0x47)) //G
        {
            gatherSelected();
            recoverOption();
            inventoryOption();
            viewClear();
            viewGather();
        }
        if (GetAsyncKeyState(0x52))//R
        {
            recoverSelected();
            gatherOption();
            inventoryOption();
            viewClear();
            viewRecover();
        }
        if (GetAsyncKeyState(0x49))//I
        {
            Item admin(0, "Test");
            inventory.addItem(admin);
            inventorySelected();
            gatherOption();
            recoverOption();
            viewClear();
            Item bannana(3, "Bannana");
            inventory.addItem(bannana);
            viewInventory(inventory);



        }
        if (GetAsyncKeyState(VK_ESCAPE))//Esc
        {
            inventoryOption();
            gatherOption();
            recoverOption();
            viewClear();
        }

    }




}

Item.h File! Item.h文件!

#include <string>

#pragma once

class Item
{
public:

    int getId();
    int getQuantity();
    std::string getName();

    void setQuantity(int set);

    Item(int id, std::string name);

private :

    int id;
    std::string name;
    int quantity;


};

Item::Item(int id, std::string name)
{
    Item::id = id;
    Item::name = name;

}
int Item::getQuantity()
{
    return quantity;
}
int Item::getId()
{
    return id;
}

std::string Item::getName()
{
    return name;
}

void Item::setQuantity(int set)
{
    quantity = set;
}

Inventory.h Header File! Inventory.h标头文件!

#include "Item.h"
#include <vector>
#include <iostream>

#pragma once

class Inventory
{
public:

    Inventory();
    std::vector<Item> inventory;
    void addItem(Item item);
    std::string display(Inventory inventory);






};
Inventory::Inventory()
{
}
void Inventory::addItem(Item item)
{
    int id = item.getId();
    boolean added = false;

    for (Item check : Inventory::inventory)
    {


        if (id == check.getId())
        {

            int x = check.getQuantity();
            x++;
            check.setQuantity(x);
            std::cout << "Check!" << check.getQuantity() << check.getName();
            added = true;

        }

    }
    if (!added)
    {
        item.setQuantity(0);
        Inventory::inventory.push_back(item);
    }

}





std::string Inventory::display(Inventory inventory)
{
    std::string display = "---Inventory---\n";
    display += "\n--------------------------------------\n";
    if (Inventory::inventory.size()== 0)
        return display;
    else
    {
        for (Item item : Inventory::inventory)
        {
            display += "     Name: " + item.getName();
            display += " Id: " + std::to_string(item.getId());
            display += " Quantity: " + std::to_string(item.getQuantity());
            display += "\n--------------------------------------\n";

        }


        return display;
    }
}

Your issue is in this line: 您的问题在以下行中:

for (Item check : Inventory::inventory)

You modify check , but check is a copy of items in your inventory! 您修改了check ,但是check是您库存中项目的副本! Loop through with check being a reference instead: 循环使用check作为参考

for (Item &check : Inventory::inventory)
         ^^^

Also, when you first get an item, you set its quantity to 0 : 另外,当您第一次获得商品时,请将其数量设置为0

item.setQuantity(0);

Is this intentional? 这是故意的吗? Perhaps you mean to set it to 1 instead: 也许您的意思是将其设置为1:

item.setQuantity(1);

Or even better, add a set to 1 in the constructor! 甚至更好的是,在构造函数中将集合添加为1! (after all, when an item is made it's because we have at least one of it, right?). (毕竟,制造一个物品是因为我们至少有一个,对吧?)。 And while your at it, you can switch to a field initialization list with your constructors to make life easier as well: 同时,您可以使用构造函数切换到字段初始化列表,以简化生活:

Item::Item(int id, std::string name) : id(id), name(name), quantity(1) { }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM