cannot declare variable to be of abstract type C/C++ Origin: Application & API OSI layer 7 · Application 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 failedassignment to expression with array type — C: error: assignment to expression with array type (C/C++)assignment to express…attempting double-free — AddressSanitizer: attempting double-free on address (C/C++)attempting double-freeC2065 — MSVC: error C2065: 'x': undeclared identifier (C/C++)C2065C2259 — MSVC: error C2259: 'X': cannot instantiate abstract class (C/C++)C2259Conditional jump depends on uninitialised value — valgrind: Conditional jump or move depends on uninitialised value(s) (C/C++)Conditional jump depe…constraints not satisfied — C++20: error: constraints not satisfied for ... / no type named 'type' in 'std::enable_if' (C/C++)constraints not satis…control reaches end of non-void function — C/C++: warning: control reaches end of non-void function [-Wreturn-type] (C/C++)control reaches end o…cannot 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.