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

**Code:** `cannot declare variable to be of abstract type` · **Category:** C/C++ · **Seen on:** C/C++, GCC, Clang

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.

## 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?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/cpp/abstract-type-instantiation)
