6

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
Philipp
  • 2,376
  • 5
  • 29
  • 47
  • 6
    How would the compiler know how to "print" a `Item` object? You need to overload the `operator<<` function for your `Item` structure. All this should be explained well in the book or tutorial you're reading, or explained in the class you're attending. Same with comparison, how would the compiler know how to compare an `Item` object with a string? – Some programmer dude Mar 21 '19 at 11:57
  • Yeah about that... my teacher isn't here right now, he got in an accident, and he doesn't usually explain much to us. We also don't have any programming books assigned to us for reading, nor do we have any tutorials. – Gamezforrealz Mar 21 '19 at 12:00
  • 4
    [Here's a curated list of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). I suggest you get a couple of them to read, and it should help with the parts that your teacher doesn't explain. – Some programmer dude Mar 21 '19 at 12:01

1 Answers1

12

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.

Thomas Sablik
  • 16,127
  • 7
  • 34
  • 62