C++ 无法声明字段';配对';为抽象类型';系统&x27;
我的C++ 无法声明字段';配对';为抽象类型';系统&x27;,c++,abstract-class,virtual,abstract,pure-virtual,C++,Abstract Class,Virtual,Abstract,Pure Virtual,我的SystemManager有一个System类的映射,其中每个系统映射到类型systype typedef string systype; 在头文件中,声明此映射: class SystemManager { public: SystemManager(); ~SystemManager(); map<systype, System> systems; System* getSystemPointer(
SystemManager
有一个System
类的映射,其中每个系统映射到类型systype
typedef string systype;
在头文件中,声明此映射
:
class SystemManager
{
public:
SystemManager();
~SystemManager();
map<systype, System> systems;
System* getSystemPointer(systype);
};
这给了我一个错误:
无法将字段“pair::second
”声明为抽象类型System
我不知道是什么引起的
以下是我的System
和DrawSystem
课程,以备不时之需:
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*>);
};
类系统
{
公众:
系统();
系统类型;
向量args;
虚空更新(向量)=0;//=0表示纯虚函数
};
类别系统:公共系统
{
朋友类游戏;//允许在渲染窗口上绘制
公众:
绘图系统();
无效更新(向量);
};
当您按值(映射系统;
)在以下行中存储系统时:
systems["Draw"] = DrawSystem();
发生时,您实际上正在尝试创建一个抽象的系统
实例
这里最简单的修复方法是切换到指针:
map<systype, System*> systems;
以及:
系统是一个抽象的类,因为他有纯粹的虚拟功能。无法创建系统的实例。
试着做以下事情
map<systype, System*> systems;
...
systems["Draw"] = new DrawSystem();
...
~SystemManager()
{
// call delete for each item in systems
}
map系统;
...
系统[“绘图”]=新的绘图系统();
...
~SystemManager()
{
//为系统中的每个项目调用delete
}
map<systype, unique_ptr<System>> systems; //pre C++11: put an extra space between >>
systems["Draw"] = unique_ptr<DrawSystem>(new DrawSystem());
systems["Draw"] = std::make_unique<DrawSystem>();
map<systype, System*> systems;
...
systems["Draw"] = new DrawSystem();
...
~SystemManager()
{
// call delete for each item in systems
}