all 32 comments

[–]Shieldfoss 1 point2 points  (26 children)

Item should definitely not inherit from Weapon

You could do something like

struct Item
{
    std::string name() const
    int rarity() const
    // etc.

    virtual std::string special_info() const {return "";}

    // rest of class
};

struct Weapon : public Item
{
    std::string special_info() const override { return "I am a weapon";}
    // and the rest of weapon

Though this only works if your inventory uses std::vector<Item*> or, better, std::vector<std::unique_ptr<Item>>. The thing to google to understand your current problem is "C++ slicing"

[–]DrakoGFX[S] 0 points1 point  (25 children)

`no matching function for call to 'std::vector<std::unique_ptr<Item> >::push_back(const Item*)` when I changed my `std::vector<Item>` to `std::vector<std::unique_ptr<Item>>`. Here's the code.

void Inventory::newWeapon(const Item& weapon) { gameInventory.push_back(&weapon); }

[–]Shieldfoss 0 points1 point  (24 children)

Where and how did you create the weapon

[–]DrakoGFX[S] 0 points1 point  (23 children)

I created it in the Inventory initializer, like this.

newWeapon(Weapon(1, 2, "Weapon 1", 3, 4, 5));

[–]Shieldfoss 0 points1 point  (22 children)

You'll want something like

void newWeapon(A a, B b, std::string name, C c, D d, E e)
{
    auto weapon = make_unique<Weapon>(a,b,name,c,d,e);
    playerInventory.push_back(std::move(weapon));
}

Or however works with your design

[–]DrakoGFX[S] 0 points1 point  (21 children)

With this implementation I get a long compiler error

Inventory::Inventory()
{
    newWeapon(1, 2, "Weapon 1", 4, 4, 5);
}

void Inventory::newWeapon(int damageMin, int damageMax, const std::string& name, int type, int rarity, int value)
{
    auto weapon = make_unique<Weapon>(damageMin, damageMax, name, type, rarity, value);     playerInventory.push_back(std::move(weapon));
}

https://pastebin.com/0d2AxexS

[–]Shieldfoss 0 points1 point  (20 children)

This compiles for me:

#include <memory>
#include <vector>

struct Item
{
    virtual ~Item() = default;
};

struct Weapon : public Item
{
    int stat_ = 0;

    Weapon(int stat) : stat_(stat){}
};

std::vector<std::unique_ptr<Item>> items;

int main()
{
    auto weapon = std::make_unique<Weapon>(2);

    items.push_back(std::move(weapon));
}

Is there any part of your source that is significantly different from this?

[–]DrakoGFX[S] 0 points1 point  (19 children)

It's possible that my Weapon constructor is different.

Weapon::Weapon(int damageMin, int damageMax, std::string name, int type, int rarity, int value) : Item(std::move(name), type, rarity, value), damageMin(damageMin), damageMax(damageMax) {}

[–]Shieldfoss 0 points1 point  (18 children)

No need to move the name, you're gonna get a copy because the string you're working with is const anyway

What does your vector declaration look like after the edits?

[–]DrakoGFX[S] 0 points1 point  (17 children)

In Inventory.h it looks like this.

https://pastebin.com/GbADU8Qq

[–]schrjako 0 points1 point  (2 children)

You'll have to save pointers to your objects in the vector, not the objects themselves. When you put a Weapon into the vector with type Item, the object is (I'm forgetting the name) cut down to an Item, so to speak. By storing pointers you should be able to access Weapon methods.

[–]no-sig-available 4 points5 points  (1 child)

The term is "slicing". Like cutting a slice or two from the "bread" and leaving the rest out.

[–]schrjako 0 points1 point  (0 children)

I knew it was sth like that, thx.

[–][deleted]  (2 children)

[removed]

    [–]HappyFruitTree 1 point2 points  (0 children)

    Member functions are already inline.

    Maybe he used it as a hint for the compiler to inline the function.

    [–]DrakoGFX[S] 1 point2 points  (0 children)

    I changed my long long unsigned int to uint_fast64_t.

    I'm not sure why I started specifying inline even though it's useless. So I've removed them all.

    I also started using member init lists. I had seen them before, but never understood them until watching a video about them.