0

I have a class A that has objects of type C, D, E, F, etc. in it, all of which inherit the base class B. I am trying to include the header for class B, but my code won't compile since I have a pure virtual function in B

Classes of type C, D, E, F, etc override the virtual function, but including the class B header in the class A header to allow for std::vector<B> objects; causes an issue. Is there any way around this? I know I can make the function not virtual, but I want to keep the base class as is and be able to use the name in class A for polymorphism

A.h:

#include "B.h"

class A {
private:
    std::vector<B> objects;
// ...
}

B.h:

class B {
public:
    virtual void doSomething() = 0;
// ...
}
Reid Moffat
  • 322
  • 1
  • 3
  • 16
  • 2
    Should it be a vector of pointers to B? You can't create objects of type B to populate the vector. – John Ilacqua Nov 10 '21 at 00:34
  • 1
    It's not the inclusion. You can't have a `vector` if `T` is an abstract type. – Nicol Bolas Nov 10 '21 at 00:34
  • In C++, polymorphic objects *requires* the use of pointers (including smart pointers) or references. You cannot have polymorphism in C++ otherwise. – Eljay Nov 10 '21 at 02:16

0 Answers0