Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Text-Based RPG Inventory System

I'm currently taking a programming 2 class (c++), we've been tasked to make a text based rpg. I'm using this post as a reference for my inventory system, as I think it's pretty effective. But I keep running into a E0349 "no operator "==" or "<<" matches these opperands" error.

If anyone could help me that would be great. Here is my full set of code:

#include "pch.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <ostream>
#include <Windows.h>
#include <string>
#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;
  };

int main()
{

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


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

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

    cout << "You sit your bag down and take a look inside." << " You have:" << endl;

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

}

Here is where my errors are more specifically

for (int i = 0; i < itemcounter; i++)  //Display equipped
{
    cout << InventorySlots[i];
    if (Equipment[i] == "Empty ") //Error Here
    {
    cout << " " << Equipment[i] << endl << endl; //Errore Here
  }
}

Error Message

Error (active)  E0349   no operator "<<" matches these operands     C:\Users\USER\source\repos\clunkinv\clunkinv\clunkinv.cpp   47
like image 426
Gamezforrealz Avatar asked Mar 21 '19 11:03

Gamezforrealz


1 Answers

Equipment[i] is an object of type Item. If you don't provide a method to compare your object with "Empty" the compiler can't know how to compare in line

if (Equipment[i] == "Empty ")

Either you compare the property

if (Equipment[i].name == "Empty ")

or you have to provide a method. Same problem in line

cout << " " << Equipment[i] << endl << endl;

The compiler can't know how to print your object. You have to provide a function for this.

You could

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;
}

You have to overload operators for your classes if you want to use them.

like image 53
Thomas Sablik Avatar answered Sep 20 '22 23:09

Thomas Sablik