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.