Wolff wrote: |
---|
1 2 3
|
//maybe like this?
std::shared_ptr<Pen> p;
std::shared_ptr<Pencil> pe;
|
|
those two pointers are not pointing at anything. Presumably, you meant for the first one to be pointing at a Pen, and for the second one to be pointing at a Pencil:
1 2
|
std::shared_ptr<Pen> p = std::make_shared<Pen>();
std::shared_ptr<Pencil> pe = std::make_shared<Pencil>();
|
or, not repeating yourself,
1 2
|
auto p = std::make_shared<Pen>();
auto pe = std::make_shared<Pencil>();
|
next,
Wolff wrote: |
---|
void AddElement(std::shared_ptr<Accesory> a) {lis.push_back(std::make_shared<Accesory>(1));}; |
presumably, you wanted to add
a
to the list, not forget
a
and create some totally new shared pointer.
So,
void AddElement(std::shared_ptr<Accesory> a) {lis.push_back(a);};
After those two edits, the linker should give you an incomprehensible error such as
prog.cc:(.text._ZN8AccesoryC2Ev[_ZN8AccesoryC5Ev]+0x9): undefined reference to `vtable for Accesory'
You can google that message to find out what it means: in this case, it says Accessory::Filling was declared, but never defined.
You can either define it
virtual void Filling() { };
or declare it as pure virtual:
virtual void Filling() = 0;
With those 3 fixes, your program compiles and runs: live demo
https://wandbox.org/permlink/IwuQSRGPRkr2PVXM (I added the missing call to "b.Summarise();" and added "for(auto& p: lis) p->Filling(); " to Summarize so that it actually prints what you wanted).
There are a few ways to improve this program. I would start by throwing away shared pointers (you don't have multithreading here or any other sort of shared ownership) and re-writing it in unique pointers.