cannot declare variable to be of abstract type C/C++ Origin: Application & API Last updated 15 Oct 2026

C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions)

You tried to create an object of a class that still has pure virtual functions; only a derived class that overrides all of them can be instantiated.

Exact message you may see cannot declare variable to be of abstract typeinvalid abstract return typebecause the following virtual functions are pure withincannot allocate an object of abstract typeobject of abstract class type is not allowed
Seen on: C/C++ GCC Clang

Meaning

A class with virtual void f() = 0; is abstract. The note under the error lists the pure virtual function that remains unimplemented, which often means a typo or a mismatch (missing const, different parameter type) so the derived function does not override.

Common causes

  • Instantiating the base class directly
  • Derived class forgot to override one pure virtual function
  • Override signature differs (const, parameter type, reference qualifier)
  • Returning or passing an abstract class by value (object slicing)

⚡ Quick fix

  1. Create the derived class instead, or hold a pointer/reference to the base
  2. Mark overrides with override so mismatches become errors
  3. Return std::unique_ptr<Base> rather than Base by value

Detailed fix by platform

C++

  1. Add override to each overriding method — the compiler then tells you which one does not match
  2. Use std::vector<std::unique_ptr<Shape>> for collections of polymorphic objects

Code examples

override catches mismatches

cpp
struct Shape {
    virtual double area() const = 0;
    virtual ~Shape() = default;
};

struct Circle : Shape {
    double r = 1;
    double area() const override { return 3.14159 * r * r; }
};

int main() {
    // Shape s;                       // error: cannot declare variable 's' to be of abstract type 'Shape'
    Circle c;                         // OK
    const Shape& s = c;
}

How to diagnose

  1. Note — Which pure virtual function is listed?
  2. Signature — Does the override match exactly?
  3. Storage — Is a base class stored by value?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

Assertion failed — C/C++: Assertion `x > 0' failed. Aborted (core dumped) — assert() condition was false (C/C++)Assertion failedC2065 — MSVC: error C2065: 'x': undeclared identifier (C/C++)C2065control reaches end of non-void function — C/C++: warning: control reaches end of non-void function [-Wreturn-type] (C/C++)control reaches end o…discards qualifiers — C++: error: passing 'const X' as 'this' argument discards qualifiers [-fpermissive] (C/C++)discards qualifiersFloating point exception — C/C++: Floating point exception (core dumped) — SIGFPE, usually integer division by zero (C/C++)Floating point except…format expects argument of type — C: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=] (C/C++)format expects argume…GLIBCXX not found — Linux: version `GLIBCXX_3.4.30' not found (required by ./app) — libstdc++.so.6 (C/C++)GLIBCXX not foundincomplete type — C++: error: invalid use of incomplete type 'class X' — forward declaration only (C/C++)incomplete typecannot de…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.