List of shared_ptr

Hi, I'm trying to learn about lists and shared pointers, and for that I'm trying to write this program as an excersize. The program should return "Gel" five times and "graphite" five times. I've stumbled a lot in my code and I'm sorry for that, I've looked in various places but have not found the implementations of lists and shared pointers in such way (at least I havn't found ones that I would understand). Thank you for all the help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <list>
#include <memory>
class Accesory
{
public:
    virtual void Filling();
};
class Pen :public Accesory
{
public:
    void Filling() {std::cout<<"Gel"<<std::endl;}
};
class Pencil :public Accesory
{
public:
    void Filling() {std::cout<<"Graphite"<<std::endl;}
};
class Desk
{
  std::list<std::shared_ptr<Accesory>> lis;
  public:
      void AddElement(std::shared_ptr<Accesory> a) {lis.push_back(std::make_shared<Accesory>(1));};
      void Summarise() {std::cout<<"In my desk are: "<<std::endl;};
};
int main()
{
Desk b;
Pen d;
//maybe like this?
std::shared_ptr<Pen> p;
std::shared_ptr<Pencil> pe;
for(int i=0; i<5; i++)
{
    b.AddElement(p);
}
for(int i=0; i<5; i++)
{
    b.AddElement(pe);
}
/*Summerise function should start the Filling function for each object in the list (so it reads out on std::cout Gel five times and Graphite 5 times */
return 0;
}
Last edited on
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.
Last edited on
Thank you so much! I know shared pointers aren't necessary but that's just the assigment (to use them this way).
Topic archived. No new replies allowed.