Want to have a class store a vector of smart pointers to an abstract class. ("cannot instantiate abstract class")

  abstract-class, c++, c++17, inheritance, polymorphism

I want to have a class containing a vector of shared pointers to an abstract class, which on runtime will be derived class objects. Here’s the abstract class:

#pragma once
#include <string_view>
#include <string>

using std::string_view; using std::string;
class Someone
{
private:
    string _name;
    unsigned _skill;

protected:
    Someone (string_view name, unsigned skill) : _name{ name }, _skill{ skill }
    { }

    Someone (const Someone& another) : _name{ another._name }, _skill{ another._skill }
    { }

    virtual ~Someone () = default;
public:

    string_view getName () const { return string_view (_name); }
    Someone& setName (string_view name) { _name = name; return *this; }

    unsigned getSkill () const { return _skill; }
    Someone& setSkill (unsigned skill) { _skill = skill; return *this; }

    string who () const { return "Name:" + _name + " has a skill level of " + std::to_string (_skill); }

    virtual string_view play () const = 0;//pure virtual function
};

Bellow, are the two simple Derived classes:

#pragma once
#include "Someone.h"
class Newbie : public Someone
{
public:
    Newbie (string_view name, unsigned skill) : Someone (name, skill)
    { }

    Newbie (const Newbie& another) : Someone (another)
    { }

    virtual string_view play () const override { return "Meh"; }
};

And

#pragma once
#include "Someone.h"
class Pro : public Someone
{
public:
    Pro (string_view name, unsigned skill) : Someone (name, skill)
    { }

    Pro (const Pro& another) : Someone (another)
    { }

    virtual string_view play () const override { return "Wow!"; }
};

And here’s the class the causes the errors. As soon as I uncomment any of the push_back lines, I get the error "Someone:" cannot instantiate abstract class.

class Everyone
{
private:
    vector<shared_ptr<Someone>> _roster;
public:

    Everyone () = default;

    Everyone (const Someone& first)
    {
        _roster.push_back (make_shared<Someone> (first));
    }

    Everyone (initializer_list<Someone> all)
    {
        for (const Someone& one : all)
        {
            //_roster.push_back (make_shared<Someone> (one));
        }
    }

    Everyone& addSomeone (const Someone& someone)
    {
        //_roster.push_back (make_shared<Someone> (someone));
        return *this;
    }
};

Source: Windows Questions C++

LEAVE A COMMENT