Mapping object containing ptr_map of abstract base classes

I have boost::ptr_mapone that stores an abstract base class (e.g. VectorWrapperBase ) as values, and this allows me to map strings to vectors of different types.

boost::ptr_map<std::string, VectorWrapperBase> memory_map;
//...
memory_map.insert(str_key, new VectorWrapper<T>());

It works. However, when I have memory_mapas a member of another class and attempt to save this class in std::map, compilation fails.

class AgentMemory {
  //...
  private:
    boost::ptr_map<std::string, VectorWrapperBase> memory_map;
};

std::map<std::string, AgentMemory> agent_map;
//...
agent_map.insert(std::pair<std::string, AgentMemory>(agent_name, AgentMemory()));

Last line failed:

/SOMEPATH/boost_1_48_0/boost/ptr_container/clone_allocator.hpp:34
   error: cannot allocate an object of abstract type ‘xyz::VectorWrapperBase’

Being new to C ++, this is puzzling.

I suspect the error boils down to inserting a map copying an object AgentMemorythat includes cloning ptr_map. And since my objects are VectorWrappernot cloneable , an error occurs.

My questions:

  • ? ( , ?)
  • ?

, , ++ , :

  • (= 0), VectorWrapperBase
    • , VectorWrapperBase .
  • VectorWrappers cloneable
    • , , , VectorWrappers ptr_map . , .
  • ptr_map std::map shared_ptr.
    • , , . (, ?) shared_ptr .
+5
1

agent_map.insert(std::pair<std::string, AgentMemory>(agent_name, AgentMemory()));

AgentMemory, , , boost::ptr_map<std::string, VectorWrapperBase>, VectorWrapperBase.

, , , VectorWrapperBase, . 3 ( ) , .

0

All Articles