Full Error:
Error 2 error C2678: binary '==' : no operator found which takes a left-hand operand of type 'Item' (or there is no acceptable conversion) c:\program files (x86)\microsoft visual studio 12.0\vc\include\algorithm 1734 1 GameStore
Inventory Class (cpp file)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <Windows.h>
#include "item.h"
class Inventory
{
public:
void Inventory::viewInventory()
{
for (int i = 0; i < inventory.size(); i++)
{
Inventory::inventory[i].getItemName();
}
}
void Inventory::addItem(Item& item)
{
inventory.push_back(item);
}
void Inventory::removeItem(Item& item)
{
if (std::find(inventory.begin(), inventory.end(), item) != inventory.end())
{
inventory.erase(std::remove(inventory.begin(), inventory.end(), item), inventory.end());
}
else
{
std::cout << "Item does not exist" << std::endl;
}
}
//Player Gold methods
int Inventory::getGold()
{
return playerGold;
}
void Inventory::setGold(int newGold)
{
playerGold = newGold;
}
int Inventory::addGold(int newGold)
{
playerGold += newGold;
return playerGold;
}
int Inventory::removeGold(int newGold)
{
playerGold -= newGold;
return playerGold;
}
//Player Gold methods
private:
std::vector<Item> inventory = {};
int playerGold;
};
Basically what I am trying to do is make an inventory system that holds objects from the class "Item". I spent a long time getting my for loops and all of my methods to work, then as the sky has never been more clear I get an error from which is way out out my league.
Item Class (CPP)
#include "item.h"
#include <iostream>
#include <string>
Item::Item(int id, std::string name, std::string description, std::string examime)
{
itemName = name;
itemID = id;
itemDescription = description;
itemExamine = examime;
}
void Item::setItemName(std::string newName)
{
itemName = newName;
}
void Item::setItemDescription(std::string newDescription)
{
itemDescription = newDescription;
}
void Item::setItemExamine(std::string newExamine)
{
itemExamine = newExamine;
}
void Item::setItemID(int newID)
{
itemID = newID;
}
std::string Item::getItemName()
{
return itemName;
}
std::string Item::getItemDescription()
{
return itemDescription;
}
std::string Item::getItemExamine()
{
return itemExamine;
}
int Item::getItemID()
{
return itemID;
}
Item Class (header)
#include <iostream>
#include <string>
class Item
{
public:
//constructor
Item::Item(int id, std::string name, std::string description, std::string examime);
//setters
void setItemName(std::string newName);
void Item::setItemDescription(std::string newDescription);
void Item::setItemExamine(std::string newExamine);
void Item::setItemID(int newID);
//getters
std::string Item::getItemName();
std::string Item::getItemDescription();
std::string Item::getItemExamine();
int Item::getItemID();
private:
std::string itemName;
int itemID;
std::string itemDescription;
std::string itemExamine;
};
If you have any advice even if it is to redo my whole system of going about this that would be great. The classes are obviously very bare and I am going to add a lot more. Meaning i'm not looking for an answer like "You don't even need an Item class"
Thanks for any help!