基于C ++文本的清单Pt.2

时间:2019-03-22 11:48:53

标签: c++

首先,我要感谢昨天为我提供帮助的每个人,但另一个错误。由于意外,我的老师目前不工作,我们在课堂上没有使用任何参考书(未提供),因此,如果我的帖子格式不正确或如果我的问题很明显,其他所有人。添加更多行之后,我现在在代码中出现两个错误(使用此post作为我自己的项目的参考。)我遇到的两个错误是:

Error (active)  E0349   no operator "=" matches these operands operand types are: Item = std::string line 76    

Error   C2679   binary '=': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) line 76 

就像我在这篇文章的开头所述,我们在课堂上被分配了这个RPG项目,但是我的老师出了事故,并且没有可用的帮助(我们的替代品是退休的女士,她基本上是职业替代品,没有编码经验)。

这是我所有的代码:

// clunkinv.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include "pch.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <ostream>
#include <Windows.h>
#include <string>
#include <cstring>
#include <cctype>
using namespace std;

struct Item {
    string name; //Item name.
    int slot; //Head, Torso, Hands
    int attack;
    int knowledge;
    int defense;
    int hp;
    int speed;
    int charisma;
};

std::ostream &operator<<(std::ostream &os, const Item& item) {
    os << item.name;
    return os;
}

    int main()
    {

        //Variables, Strings, etc.
        int itemcounter = 0, counter = 0;
        string search;


        //"Empty" Item
        Item Empty{ "<Empty>", 0, 0, 0 };

    Item Sword{
        "Paper Cutter Sword", //This item name is 'Short Sword'.
        3, //Slot
        3, //Attack
        0,
        0,
        0,
        0,
        0,
        };

        vector<Item> Equipment = { 6, Empty }; //Current Equipment, 6 empty slots.
        vector<Item> Inventory = { }; //Inventory
        string InventorySlots[] = { "Head" "Torso", "Hands" }; //Player slots where items can be equiped.

        cout << "You sit your bag down and take a look inside." << " You have:" << endl;
        cout << "Enter 'Equip' to equip an item, or 'inventory' to see full inventory" << endl;

        for (int i = 0; i < itemcounter; i++)
        {
            cout << InventorySlots[i];
            if (Equipment[i].name == "Empty ") 
            {
                cout << " " << Equipment[i].name << endl << endl; 
            }
        }

        if (search == "inventory") {
            cout << "What do you want to equip? ";
            cin >> search;
            for (unsigned i = 0; i < Inventory.size(); i++) {
                //Search for item player want to equip, put it in right slot.
                if (search == Inventory[i].name) {
                    Equipment[Inventory[i].slot] = Inventory[i].name;
                    cout << "Successfully equiped!" << endl;
                }
            }
        }

        if (search == "inventory") {
            for (unsigned i = 0; i < Inventory.size(); i++) {
                cout << "______________________________________________________________" << endl;
                cout << "|  " << Inventory[i].name << endl;
                cout << "|  Carried items " << Inventory.size() << " / " << 20 << endl;
                cout << "|_____________________________________________________________" << endl;
            }
        }

    }

1 个答案:

答案 0 :(得分:1)

Equipment[Inventory[i].slot] = Inventory[i].name;

在此行上,您尝试将std::stringInventory[i].name)分配给ItemEquipment[Inventory[i].slot])。

这两种类型之间没有提供转换,因此编译器不知道该怎么做。

如果仅要将ItemInventory复制到Equipment,则只需删除.name部分。

Equipment[Inventory[i].slot] = Inventory[i];