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.
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 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
- Create the derived class instead, or hold a pointer/reference to the base
- Mark overrides with
overrideso mismatches become errors - Return
std::unique_ptr<Base>rather thanBaseby value
Detailed fix by platform
C++
- Add
overrideto each overriding method — the compiler then tells you which one does not match - Use
std::vector<std::unique_ptr<Shape>>for collections of polymorphic objects
Code examples
override catches mismatches
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
- Note — Which pure virtual function is listed?
- Signature — Does the override match exactly?
- 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:
Similar errors
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 15 Oct 2026