user3191398 user3191398 - 2 months ago 14x
C++ Question

Transform vector of reference wrapper to Base class to vector of reference_wrapper to Derived class cannot dynamic cast

As in title. I got this error when trying to dynamic_cast

cannot dynamic_cast '(&
obj)->std::reference_wrapper<_Tp>::get()' (of type 'class
MEPObject') to type 'class MEPGene&' (target is not pointer or
reference to complete type)
genes.push_back(dynamic_cast (obj.get()));

class MEPObject;
class MEPGene;
typedef std::vector<std::reference_wrapper<MEPObject>> MEPObjects;
typedef std::vector<std::reference_wrapper<MEPGene>> MEPGenes;

void dynamicCast(MEPObjects &objects, MEPGenes &genes)
for(const auto &obj: objects)
genes.push_back(dynamic_cast<MEPGene&> (obj.get()));


Forward declaration is not enough.

Definition of MEPGene is required (to see inheritance).