1

My SystemManager has a map of System classes, where each system is mapped to type systype

typedef string systype;

In the header file, this map is declared:

class SystemManager
{
    public:
        SystemManager();
        ~SystemManager();

        map<systype, System> systems;

        System* getSystemPointer(systype);
};

And I try to add a DrawSystem (a class derived from System) to my "systems map" in the constructor:

SystemManager::SystemManager()
{
    systems["Draw"] = DrawSystem();
}

This gives me the error:

cannot declare filed 'pair<systype, System>::second' to be of abstract type System

I can't figure out what is causing it.

Here are my System and DrawSystem classes in case that matters:

class System
{
    public:
        System();

        systype type;
        vector<cptype> args;
        virtual void update(vector<Cp*>) = 0; //= 0 is for pure virtual function
};

class DrawSystem : public System
{
    friend class Game; //allows to draw on render window
    public:
        DrawSystem();

        void update(vector<Cp*>);
};
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Yaxlat
  • 665
  • 1
  • 10
  • 25

2 Answers2

2

When you store Systems by value (map<systype, System> systems;) in the following line:

systems["Draw"] = DrawSystem();

slicing occurs and you're actually trying to create an instance of System, which is abstract.

The simplest fix here is to switch to pointers instead:

map<systype, System*> systems;

but consider also using something like std::unique_ptr instead, to avoid manual memory management. E.g.:

map<systype, unique_ptr<System>> systems; //pre C++11: put an extra space between >>

and:

systems["Draw"] = unique_ptr<DrawSystem>(new DrawSystem());

or even better - not using new (as suggested in the comments by sjdowling):

systems["Draw"] = std::make_unique<DrawSystem>();
Community
  • 1
  • 1
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
0

System is abstact class, because he has pure virtual function. You cannot create an instance of System. Try to do following

map<systype, System*> systems;
...
systems["Draw"] = new DrawSystem();
...
~SystemManager()
{
    // call delete for each item in systems
}
Kastaneda
  • 739
  • 1
  • 8
  • 15