1
class Inventory
{
public:
    Inventory();
    ~Inventory();
private:
    //std::vector<std::shared_ptr<IItem*>> ItemList;
    std::vector<IItem*>  ItemList;

public:
    template <class T>
    void AddItem(T &item) {
        this->ItemList.push_back(*item);
    }
    std::string Test(int);
    void MoveItem(int, int);
};
    Inventory *inv = new Inventory();
    Weapon *sword = new Weapon("sword", 2, 3, "physical");
    inv->AddItem<Weapon>(*sword);

    txtTest->SetValue(inv->Test(0));
    evt.Skip();

I want to create a list of object that inherits from the virtual object named "IItem" inside of "Inventory" object. I created a vector of pointers type "IItem", as you can see above and template of method which should add pointer from the argument to my vector.

When I try to compile this I get:

Build started...
1>------ Build started: Project: PO_project, Configuration: Debug Win32 ------
1>cMain.cpp
1>D:\Programy\PO_project\PO_project\PO_project\Inventory.h(20,28): error C2100: illegal indirection
1>D:\Programy\PO_project\PO_project\PO_project\cMain.cpp(21): message : see reference to function template instantiation 'void Inventory::AddItem<Weapon>(T &)' being compiled
1>        with
1>        [
1>            T=Weapon
1>        ]
1>Done building project "PO_project.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

I have no idea how to do this because this error is somehow a thing I want to achieve. I had done something similar in C# and now I trying with C++ but you can see the effect of my work.

DonQnei
  • 55
  • 9
  • Possibly helpful: https://stackoverflow.com/questions/57483/what-are-the-differences-between-a-pointer-variable-and-a-reference-variable-in – Kostas Dec 16 '20 at 22:48
  • Please make a [mre] with the definition of `IItem`, `Weapon` etc. – cigien Dec 16 '20 at 22:48
  • I think you mean `ItemList.push_back(&item)`, not `ItemList.push_back(*item)` – Kostas Dec 16 '20 at 22:49
  • Why does your `ItemList` must be a `std::vector` and not `std::vector`? What does using pointers here, and all the headaches that go with them, like properly allocating and deallocating them, give you? – Sam Varshavchik Dec 16 '20 at 22:50
  • Taking the address would make the code compile, but it would be just as wrong because you'd easily get pointers to deleted objects, @Kostas. – Ulrich Eckhardt Dec 16 '20 at 22:50
  • @UlrichEckhardt It's really bad style I agree, but op creates these items with `new`, and I don't think he realizes `*x` doesn't cast `x` to a pointer. – Kostas Dec 16 '20 at 22:53
  • @SamVarshavchik somewhere on StackOverflow I had read if I need list or vector of objects that inherits from the virtual object I need pointers to them so I trying this. – DonQnei Dec 16 '20 at 22:56
  • That is correct, however that's an important detail that should've been mentioned in your question. – Sam Varshavchik Dec 17 '20 at 00:52

2 Answers2

2

I'd probably not use templates for this. if IItem is a base impl, then you could probably cast the object you push like so

inv->AddItem(static_cast<IItem*>(sword)) and define the AddItem method like so:

void Inventory::AddItem(IItem *itm){
  this->ItemList.push_back(itm);
}
1

You don't need anything fancy.

void AddItem(IItem* item) {
    this->ItemList.push_back(item);
}

You don't need any casts either. If Weapon is derived from IItem:

Weapon* sword = new Weapon;
inv->AddItem(sword);

I really recommend replacing raw pointers with smart pointers though.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243
  • that changed my error to something like: 1>cMain.obj : error LNK2019: unresolved external symbol "public: __thiscall Inventory::Inventory(void)" (??0Inventory@@QAE@XZ) referenced in function "public: void __thiscall cMain::OnButtonClicked(class wxCommandEvent &)" (?OnButtonClicked@cMain@@QAEXAAVwxCommandEvent@@@Z) – DonQnei Dec 16 '20 at 23:10
  • But that looks like a problem with other library – DonQnei Dec 16 '20 at 23:11
  • 1
    @DonQnei if you have another question, please post another question, or perhaps check [this](https://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix). – n. m. could be an AI Dec 16 '20 at 23:17