mardi 16 mars 2021

clang-diagnostic-error - allocating an object of abstract class type 'Base'

I get below error when I build this code.

1.

in instantiation of function template specialization 'std::make_unique<Base, Derive *>' requested here
        unique_ptr<Base> b = make_unique<Base>(new Derive());
  1. unimplemented pure virtual method 'foo' in 'Base' virtual void foo() = 0;

    --

    #include #include

    using namespace std;

    class Base { public: virtual void foo() = 0; protected: int num{ 1 };

    public: Base() { cout << "Base class constructor" << endl; } virtual ~Base() { cout << "Base class destructor " << endl; } private: };

    class Derive : public Base { public: Derive() { cout << "Derive class constructor" << endl; } ~Derive() { cout << "Derive class destructor " << endl; } public: void foo() override{ cout << "Derive class Foo function" << endl; } };

    int main() { unique_ptr b = make_unique(new Derive()); b->foo();

    }

Does anyone know what is the reason of this error?

Note: I tried on https://godbolt.org/ and selected clang 11

Aucun commentaire:

Enregistrer un commentaire