C++ 通过C+中的外部函数更改存储在另一个对象的向量中的对象中元素的值+;

C++ 通过C+中的外部函数更改存储在另一个对象的向量中的对象中元素的值+;,c++,function,object,vector,C++,Function,Object,Vector,因此,创建了一个名为“Item”的类,该类的对象在开始时将具有100%的条件,只要我告诉玩家,玩家就会存储项目(在本例中为“apple”)。在degradeT函数中,我希望传递包含玩家迄今为止拾取的项目的整个向量,然后通过chCond函数将该向量中每个项目的条件更改为-1 第一个错误: 第二个错误: “void degradeT(std::vector&”):无法将参数1从“std::vector”转换为“std::vector&” #包括“pch.h” #包括 #包括 #包括 使用std::

因此,创建了一个名为“Item”的类,该类的对象在开始时将具有100%的条件,只要我告诉玩家,玩家就会存储项目(在本例中为“apple”)。在degradeT函数中,我希望传递包含玩家迄今为止拾取的项目的整个向量,然后通过chCond函数将该向量中每个项目的条件更改为-1

第一个错误:

第二个错误:

“void degradeT(std::vector&”):无法将参数1从“std::vector”转换为“std::vector&”
#包括“pch.h”
#包括
#包括
#包括
使用std::cout;使用std::cin;使用std::endl;
使用std::string;使用std::vector;使用std::to_字符串;
类项目{
私人:
字符串名称;//项名称
浮动条件;//项条件
bool消耗品;//是消耗品吗
公众:
项(){}
项(字符串a、浮点b、布尔c){name=a;条件=b;消耗品=c;}
项目(字符串a,布尔c){name=a;条件=100.f;耗材=c;}
字符串getName(){
返回名称;
}
浮点getCond(){
返回条件;
}
bool-isCons(){
退回消耗品;
}
void chCond(float a){//更改项目条件
条件+=a;
}
};
//-----------------------
职业选手{
私人:
向量plItems;//项容器
公众:
播放器(){}
无效拾取项目(项目a){//将项目添加到库存
plItems.推回(a);

cout我不确定您的问题是什么,但您的错误与函数
void degradeT(vector&Itemss)
有关

此函数需要引用,但您正在传递r值。您可以使用
getPlItems()
返回引用,也可以将l值传递给
degradeT

initial value of reference to non-const must be an lvalue
'void degradeT(std::vector<Item,std::allocator<_Ty>> &)': cannot convert argument 1 from 'std::vector<Item,std::allocator<_Ty>>' to 'std::vector<Item,std::allocator<_Ty>> &'
#include "pch.h"
#include <iostream>
#include <string>
#include <vector>

using std::cout; using std::cin; using std::endl;
using std::string; using std::vector; using std::to_string;

class Item {

private:
    string name; // Item name
    float condition; // Item condition
    bool consumable; // Is the item consumable
public:
    Item() {}
    Item(string a, float b, bool c) { name = a; condition = b; consumable = c; }
    Item(string a, bool c) { name = a; condition = 100.f; consumable = c; }

    string getName() {
        return name;
    }
    float getCond() {
        return condition;
    }
    bool isCons() {
        return consumable;
    }
    void chCond(float a) { // Change Item condition
        condition += a;
    }
};

//-----------------------

class Player {

private:
    vector<Item> plItems; // Item container
public:
    Player() {}

    void pickUpItem(Item a) { // Adding Items to inventory
        plItems.push_back(a);
        cout << a.getName() << " added to inventory!\n";
    }
    void checkItemConds() { // Checking condition of all items
        for (unsigned int a = 0, siz = plItems.size(); a < siz; a++) {
            cout << plItems[a].getName() << "'s condition is: " << plItems[a].getCond() << "%\n";
        }
    }
    Item returnItem(unsigned int a) { // Return a specific Item
        return plItems[a];
    }
    int getCurInvOcc() { // Get cuurent inventory occupation
        return plItems.size();
    }
    vector<Item> getPlItems() { // Return the vector (Item container)
        return plItems;
    }
};

//-------------------------
void degradeT(vector<Item>& Itemss); // Degrade item after some time
//-------------------------

int main()
{
    Player me; // me
    string inp; // input
    int num = 1; // apple 1, apple 2, apple 3...

    while (inp != "exit") {
        cin >> inp;

        if (inp == "addApple") {
            Item apple(("apple " + to_string(num)), true);
            me.pickUpItem(apple);
            num++;
        }

        if (inp == "checkItemConds") {
            me.checkItemConds();
        }

        if (inp == "timeTick") {

            // This doesn't have anything to do with time I just want to test the function manually

            degradeT(me.getPlItems());
        }

    }

    system("PAUSE");
    return 0;
}

void degradeT(vector<Item> &Itemss) {

    for (unsigned int a = 0, siz = Itemss.size(); a < siz; a++) {
        Itemss[a].chCond(-1);
        cout << Itemss[a].getName() << endl;
    }
}