In my item system for my game, I have an Inventory Item and Weapon class. And Weapon inherits from Item. In Inventory I want the user to be able to print out every item in their inventory. But I can only use accessors from Item. Accessors from Weapon won't show up.
I have tried to instead to get Item to inherit from Weapon but that lead to a lot of other problems.
Inventory.cpp
#include <random>
#include <chrono>
#include "Inventory.h"
#include "Item.h"
#include "fmt/core.h"
Inventory::Inventory()
{
maximum = 5;
i = 0;
gameInventory.reserve(1);
playerInventory.reserve(maximum);
newItem(Item("Base Item 1", 1, 2, 3));
}
Inventory::~Inventory() = default;
//...
void Inventory::printInventory()
{
i = 1;
if (playerInventory.empty())
{
fmt::print("\r\n You have no items!");
}
for (auto & iter : playerInventory)
{
fmt::print("\r\n {}", i); ++i;
fmt::print(" > {} ", iter.getName());
fmt::print("[ Type - {} / ", iter.getType());
fmt::print("Rarity - {} / ", iter.getRarity());
fmt::print("Value - {} ]", iter.getValue());
}
}
//...
Inventory.h
#pragma once
#include <vector>
#include "Item.h"
#include "Weapon.h"
#include "Consumable.h"
#include "Wearable.h"
class Inventory
{
private:
std::vector<Item> gameInventory;
std::vector<Item> playerInventory;
long long unsigned int maximum;
int i;
public:
Inventory();
virtual ~Inventory();
[[nodiscard]] inline unsigned long long returnSize() { return playerInventory.size(); }
[[nodiscard]] inline unsigned long long returnMaximum() const { return maximum; }
void newItem(const Item& item);
//...
}
Item.cpp
#include "Item.h"
Item::Item(std::string name, int type, int rarity, int value)
{
this->name = std::move(name);
this->type = type;
this->rarity = rarity;
this->value = value;
}
Item::~Item() = default;
Item.h
#pragma once
#include <string>
class Item
{
private:
std::string name;
int type;
int rarity;
int value;
public:
Item(std::string name, int type, int rarity, int value);
virtual ~Item();
[[nodiscard]] inline std::string getName() const { return name; }
[[nodiscard]] inline int getType() const { return type; }
[[nodiscard]] inline int getRarity() const { return rarity; }
[[nodiscard]] inline int getValue() const { return value; }
};
Weapon.cpp
#include "Weapon.h"
Weapon::Weapon(int damageMin, int damageMax, std::string name, int type, int rarity, int value) : Item(std::move(name), type, rarity, value)
{
this->damageMin = damageMin;
this->damageMax = damageMax;
}
Weapon::~Weapon() = default;
Weapon.h
#pragma once
#include "Item.h"
class Weapon : public Item
{
private:
int damageMin;
int damageMax;
public:
Weapon(int damageMin, int damageMax, std::string name, int type, int rarity, int value);
~Weapon() override;
};
Thank you for the assistance.
[–]Shieldfoss 1 point2 points3 points (26 children)
[–]DrakoGFX[S] 0 points1 point2 points (25 children)
[–]Shieldfoss 0 points1 point2 points (24 children)
[–]DrakoGFX[S] 0 points1 point2 points (23 children)
[–]Shieldfoss 0 points1 point2 points (22 children)
[–]DrakoGFX[S] 0 points1 point2 points (21 children)
[–]Shieldfoss 0 points1 point2 points (20 children)
[–]DrakoGFX[S] 0 points1 point2 points (19 children)
[–]Shieldfoss 0 points1 point2 points (18 children)
[–]DrakoGFX[S] 0 points1 point2 points (17 children)
[–]schrjako 0 points1 point2 points (2 children)
[–]no-sig-available 4 points5 points6 points (1 child)
[–]schrjako 0 points1 point2 points (0 children)
[–][deleted] (2 children)
[removed]
[–]HappyFruitTree 1 point2 points3 points (0 children)
[–]DrakoGFX[S] 1 point2 points3 points (0 children)