Ok so I compiled the code on my chromebook in an android compiler, and i copied it EXACTLY as it was written on my chromebook to Visual studio 2019 on my windows desktop and i'm getting this weird error for some reason. Both VS 2019 and the chromebook app are using c++ 17 standard, so I have no idea why its not working, the error is in here:
Code:
void Character::AddToInventory(Item& item, int amount)
{
auto iter = inventory.find(item.GetName()); //This line is causing the error
if (iter != inventory.end())
{
iter->second += amount;
}
else
{
inventory.emplace(item.GetName(), amount);
}
}
The errors are:
Code:
Error C3536 'iter': cannot be used before it is initialized
Error C2664 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const Item,int>>>> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::find(const Item &)': cannot convert argument 1 from 'std::string' to 'const Item &'
Error C2677 binary '!=': no global operator found which takes type 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const Item,int>>>>' (or there is no acceptable conversion)
Error (active) E0304 no instance of overloaded function "std::map<_Kty, _Ty, _Pr, _Alloc>::find [with _Kty=Item, _Ty=int, _Pr=std::less<Item>, _Alloc=std::allocator<std::pair<const Item, int>>]" matches the argument list
I must be missing something, The chromebook code compiles in VS but this doesnt, Do I have to put map<Item, int> in the AddToItem function argument list instead of Item& item for this to work?
I also noticed that the code i compiled on my chromebook, it doesnt exactly work correctly, after adding a few items to it, it will add multiple of the same items to the inventory instead of adding it once and incrementing the amount, but perhaps thats because i didnt use a map like i mentioned above?
Here is my VS 2019 code:
Here is the VS 2019 project files if its easier: Basic Inheritance Example
main.cpp
Code:
#include <iostream>
#include <string>
#include <tuple>
#include "Item Class.h"
#include "Weapon Class.h"
#include "Sword Class.h"
#include "Character.h"
#include "Player.h"
int main()
{
//Create our weapons
Sword MasterSword{ "Master Sword", 1000, 500, 35, 60, 100};
//Create our items
Item Gold("Gold", 1, 1, 1);
Item WeakPotion("Weak Potion", 40, 25, 2);
Player Link("Link", 100);
Link.AddToInventory(Gold, 300);
//Link.AddToInventory(Gold, 30);
std::cout << Link.GetName() << "'s inventory\n" << std::endl;
Link.ShowInventory();
std::cout << "\n";
return 0;
}
Character.h
Code:
#pragma once
#include <iostream>
#include <map>
#include <iterator>
#include "Item Class.h"
using std::map;
using std::find;
using std::string;
using std::cout;
using std::endl;
using std::ostream;
class Character
{
public:
Character(const string& name, float health)
: m_name(name), m_health(health)
{}
string GetName() const { return m_name; }
float GetHealth() const { return m_health; }
int GetItemStock() const { return m_itemStock; }
friend ostream& operator<<(ostream& os, const Character& character)
{
os << "Name: " << character.GetName() << endl;
//os << "Health: " << character.GetHealth() << endl;
return os;
}
void ShowInventory();
void AddToInventory(Item& item, int amount);
void RemoveItemFromInventory(Item& item, int amount);
private:
string m_name{ "Character" };
float m_health{ 100 };
map<Item, int> inventory{};
int m_itemStock{ 0 };
};
character.cpp
Code:
#include "Character.h"
void Character::AddToInventory(Item& item, int amount)
{
auto iter = inventory.find(item.GetName());
if (iter != inventory.end())
{
iter->second += amount;
}
else
{
inventory.emplace(item.GetName(), amount);
}
}
void Character::ShowInventory()
{
map<Item, int>::iterator it = inventory.begin();
for (auto& i : inventory)
{
std::cout << it->first << " x" << it->second << std::endl;
}
}
void Character::RemoveItemFromInventory(Item& item, int amount)
{
}
player.h
Code:
#pragma once
#include "Character.h"
#include "Item Class.h"
class Player : public Character
{
public:
Player(const std::string& name, float health)
: Character{ name, health}
{}
};
weapon class.h
Code:
#pragma once
#include "Item Class.h"
class Weapon : public Item
{
public:
Weapon(const std::string& name, float cost, float value, float weight, float attackPower, float durability)
: Item{ name, cost, value, weight}, m_attackPower(attackPower), m_durability(durability)
{}
friend std::ostream& operator<<(std::ostream& os, const Weapon& weapon)
{
os << static_cast<const Item&>(weapon);
os << "Attack Power: " << weapon.m_attackPower << std::endl;
os << "Durability: " << weapon.m_durability << std::endl;
return os;
}
float GetAttackPower() const { return m_attackPower; }
float GetDurability() const { return m_durability; }
private:
float m_attackPower{ 5 };
float m_durability{ 1 };
};
Sword class.h
Code:
#pragma once
#include "Weapon Class.h"
class Sword : public Weapon
{
public:
/*Sword(const std::string& name, float cost, float value, float weight, float attackPower, float durability)
: Weapon{ name, cost, value, weight, attackPower, durability }
{}*/
//Just inherit weapon constructor since we arent creating any new variables here yet.
Weapon::Weapon;
};
item class.h
Code:
#pragma once
using std::endl;
using std::string;
class Item
{
public:
Item(const string& name, float cost, float value, float weight)
: m_name(name), m_cost(cost), m_value(value), m_weight(weight)
{}
string GetName() const { return m_name; }
float GetCost() const { return m_cost; }
float GetValue() const { return m_value; }
float GetWeight() const { return m_weight; }
friend std::ostream& operator<<(std::ostream& os, const Item& item)
{
os << item.GetName();
//os << "Value: " << item.value << std::endl;
//os << "Weight: " << item.weight << std::endl;
return os;
}
friend bool operator<(const Item& a, const Item& b)
{
return a.GetName() < b.GetName();
}
friend bool operator==(const Item& l, const Item& r)
{
return l.GetName() == r.GetName();
}
protected:
string m_name{ "Generic Item" };
float m_cost{ 2 };
float m_value{ 1 };
float m_weight{ 1 };
};
Here is the code from the chromebook compile:
Code:
#include <iostream>#include <map>
#include <string>
#include <iterator>
using std::string;
using std::cout;
using std::endl;
using std::map;
using std::pair;
using std::ostream;
class Character;
class Item
{
public:
Item(const string& name): m_name(name)
{}
string GetItemName() const { return m_name; }
friend bool operator< (const Item& a, const Item& b)
{
return a.GetItemName() < b.GetItemName();
}
friend ostream& operator<<(ostream& os, const Item& item)
{
os << item.GetItemName();
return os;
}
private:
string m_name{ "Item" };
};
class Character
{
public:
Character(const string& name): m_name(name)
{}
void AddItemToInventory(Item& item, int amountToAdd);
void OpenInventory();
int GetItemInventoryAmount() const { return m_itemInventoryStock; }
private:
string m_name{ "Character" };
map<Item, int> m_inventory{};
int m_itemInventoryStock{ 0 };
};
void Character::AddItemToInventory(Item& item, int amountToAdd)
{
auto iter = m_inventory.find(item.GetItemName());
if(iter != m_inventory.end())
{
iter->second += amountToAdd;
}
else
{
m_inventory.emplace(item.GetItemName(), amountToAdd);
}
}
void Character::OpenInventory()
{
map<Item, int>::iterator it = m_inventory.begin();
for(auto& i : m_inventory)
{
cout << it->first << " x" << it->second << endl;
}
}
int main()
{
Character Link("Link");
Item Rupee("Rupee");
Link.AddItemToInventory(Rupee, 5);
Link.AddItemToInventory(Rupee, 6);
Link.OpenInventory(); }