C++
Cannot Declare Variable of Abstract Type Fix
A class with a pure virtual function that no class in its hierarchy has finally overridden cannot be created as an object. The error lands on the variable, but the cause is in the class: read the note under the error, it names the pure virtual function that is still open, then close it in a concrete derived class, add the override the derived class forgot, or fix the signature that only looks like an override.
Written November 26, 2026 · Tested with GCC 13.3.0 · 9 min read
- The first error: a variable of the abstract base itself
- The fix: create the derived class, use it through the base
- A derived class that forgets one override
- The fix: add the missing override
Written November 26, 2026
Tested with GCC 13.3.0
What this guide is
This error is the build saying the variable's type cannot be made into an object. A class is abstract when it defines or inherits at least one function whose final overrider is pure virtual, written with = 0. No objects of an abstract class can be created, except as base subobjects of a derived class. The class stays abstract until a derived class overrides every pure virtual function it inherits.
GCC names the cause directly under the error. The first note says the following virtual functions are pure within the class, and the next note quotes the declaration of the pure virtual function that is still open. That quoted declaration is the work list: implement that function, with that exact signature, in the class named in the error, or use a class that already does.
Three shapes produce the same message. The variable's type is the abstract base itself. A derived class forgets to override one of several pure virtual functions, so the derived class is still abstract. Or a derived class writes a function that looks like the override but has a different signature, so it hides the base function instead of overriding it, and the derived class is still abstract. Each section below takes one shape, shows the exact message it prints, and gives the fix that belongs to that shape.
The examples here use C++20 and GCC 13.3.0. Each program is a separate file, compiled with g++ -std=c++20 and the file name. A failing compile prints its diagnostic to standard error and exits with status 1; a passing program is run, and its output is shown exactly as printed. Each section's failing program isolates one shape, so the diagnostic in that section belongs to that shape and nothing else.
The first error: a variable of the abstract base itself
The smallest shape declares a pure virtual function in Shape, then tries to create a Shape:
#include <iostream>
struct Shape {
virtual double area() const = 0;
virtual ~Shape() = default;
};
int main() {
Shape shape;
std::cout << shape.area() << '\n';
return 0;
}
$ g++ -std=c++20 base_var.cpp -o base_var
base_var.cpp: In function 'int main()':
base_var.cpp:9:11: error: cannot declare variable 'shape' to be of abstract type 'Shape'
9 | Shape shape;
| ^~~~~
base_var.cpp:3:8: note: because the following virtual functions are pure within 'Shape':
3 | struct Shape {
| ^~~~~
base_var.cpp:4:20: note: 'virtual double Shape::area() const'
4 | virtual double area() const = 0;
| ^~~~
The error points at line 9, the variable. The notes point back into the class: area() const is pure within Shape, because line 4 declares it with = 0 and nothing overrides it. There is no implementation to call and no concrete type to construct, so the declaration of the object itself is refused. Removing = 0 and giving area() a body would also remove the error, but it changes the design: the base stops requiring derived classes to provide the function. The usual fix keeps the base abstract and creates a concrete class instead.
The fix: create the derived class, use it through the base
Pointers and references to an abstract class can be declared. Create the concrete derived class that overrides the pure virtual function, then hold it through a base reference or pointer:
#include <iostream>
struct Shape {
virtual double area() const = 0;
virtual ~Shape() = default;
};
struct Circle : Shape {
double area() const override { return 3.0; }
};
int main() {
Circle circle;
Shape& ref = circle;
Shape* ptr = &circle;
std::cout << ref.area() << ' ' << ptr->area() << '\n';
return 0;
}
$ g++ -std=c++20 base_var_fixed.cpp -o base_var_fixed
$ ./base_var_fixed
3 3
The object created is a Circle, which is concrete because its area() const overrides the pure virtual function. ref and ptr have the abstract type Shape& and Shape*, and both are allowed: neither creates a Shape object, both refer to the Circle. A call through either one dispatches to Circle::area(), which is why both print 3. When existing code is written against the base type, this is the fix that leaves that code unchanged: change what is created, not the type it is held through.
A derived class that forgets one override
A derived class does not need to miss every function to stay abstract. Missing one is enough. Here Shape has two pure virtual functions, and Circle overrides only area():
#include <iostream>
struct Shape {
virtual double area() const = 0;
virtual void name() const = 0;
virtual ~Shape() = default;
};
struct Circle : Shape {
double area() const override { return 3.0; }
};
int main() {
Circle circle;
std::cout << circle.area() << '\n';
return 0;
}
$ g++ -std=c++20 missing_override.cpp -o missing_override
missing_override.cpp: In function 'int main()':
missing_override.cpp:14:12: error: cannot declare variable 'circle' to be of abstract type 'Circle'
14 | Circle circle;
| ^~~~~~
missing_override.cpp:9:8: note: because the following virtual functions are pure within 'Circle':
9 | struct Circle : Shape {
| ^~~~~~
missing_override.cpp:5:18: note: 'virtual void Shape::name() const'
5 | virtual void name() const = 0;
| ^~~~
The class named in the error is now Circle, not Shape: the variable's own type is the abstract one. The note names only name(). It does not name area(), because Circle already overrides area(). Read that note as the exact remaining list for this class. In a larger hierarchy the list can hold more than one function, one note line each, and every one of them has to be closed before the class can be created.
The fix: add the missing override
Add the function the note names, with the signature the note quotes:
#include <iostream>
struct Shape {
virtual double area() const = 0;
virtual void name() const = 0;
virtual ~Shape() = default;
};
struct Circle : Shape {
double area() const override { return 3.0; }
void name() const override { std::cout << "circle\n"; }
};
int main() {
Circle circle;
circle.name();
std::cout << circle.area() << '\n';
return 0;
}
$ g++ -std=c++20 missing_override_fixed.cpp -o missing_override_fixed
$ ./missing_override_fixed
circle
3
With both pure virtual functions overridden, no final overrider in Circle is pure, so Circle is not abstract and the variable is allowed. The override specifier on each function asks the compiler to check that the function really does override a base virtual function, and the program is ill-formed if it does not. That check is what separates this shape from the next one.
A signature that hides instead of overriding
The third shape writes the function and still gets the error. Circle::area() here drops the const from the base declaration:
#include <iostream>
struct Shape {
virtual double area() const = 0;
virtual ~Shape() = default;
};
struct Circle : Shape {
double area() { return 3.0; }
};
int main() {
Circle circle;
std::cout << circle.area() << '\n';
return 0;
}
$ g++ -std=c++20 mismatch.cpp -o mismatch
mismatch.cpp: In function 'int main()':
mismatch.cpp:13:12: error: cannot declare variable 'circle' to be of abstract type 'Circle'
13 | Circle circle;
| ^~~~~~
mismatch.cpp:8:8: note: because the following virtual functions are pure within 'Circle':
8 | struct Circle : Shape {
| ^~~~~~
mismatch.cpp:4:20: note: 'virtual double Shape::area() const'
4 | virtual double area() const = 0;
| ^~~~
Nothing points at line 9, the Circle::area() definition. A virtual function is overridden only by a derived function with the same name, the same parameter type list, the same cv-qualifiers, and the same ref-qualifiers. area() without const differs in cv-qualifiers, so it does not override Shape::area() const. It is a separate function that hides the base function during name lookup in Circle, and the pure virtual function stays pure within Circle. The call circle.area() would find the new function, which is why this shape is easy to miss by reading the code: the function exists, it is just not the override.
The fix: match the signature and mark it override
Restore the const so the signature matches, and add override:
#include <iostream>
struct Shape {
virtual double area() const = 0;
virtual ~Shape() = default;
};
struct Circle : Shape {
double area() const override { return 3.0; }
};
int main() {
Circle circle;
std::cout << circle.area() << '\n';
return 0;
}
$ g++ -std=c++20 mismatch_fixed.cpp -o mismatch_fixed
$ ./mismatch_fixed
3
The override specifier makes the intent checkable: it ensures the function is virtual and is overriding a virtual function from a base class, and the program is ill-formed if that is not true. Adding it to every intended override turns this whole shape from a silent non-override into an error at the function itself, as the next section shows.
A neighbor that names the real cause: the same mismatch with override
Write the mismatched function with override from the start, and the compiler reports the mismatch at the function, before the abstract error:
#include <iostream>
struct Shape {
virtual double area() const = 0;
virtual ~Shape() = default;
};
struct Circle : Shape {
double area() override { return 3.0; }
};
int main() {
Circle circle;
std::cout << circle.area() << '\n';
return 0;
}
$ g++ -std=c++20 mismatch_override.cpp -o mismatch_override
mismatch_override.cpp:9:12: error: 'double Circle::area()' marked 'override', but does not override
9 | double area() override { return 3.0; }
| ^~~~
mismatch_override.cpp: In function 'int main()':
mismatch_override.cpp:13:12: error: cannot declare variable 'circle' to be of abstract type 'Circle'
13 | Circle circle;
| ^~~~~~
mismatch_override.cpp:8:8: note: because the following virtual functions are pure within 'Circle':
8 | struct Circle : Shape {
| ^~~~~~
mismatch_override.cpp:4:20: note: 'virtual double Shape::area() const'
4 | virtual double area() const = 0;
| ^~~~
This program has two errors, not one. The first, marked 'override', but does not override, is a different message and it points at the actual defect, the function on line 9. The abstract error follows because the class is still abstract. When a build shows both, fix the first error first: matching the signature clears both. When a build shows only the abstract error, as in the shape above, the mismatch is still a candidate cause, and comparing the derived signature against the quoted declaration in the note is how to find it.
A neighbor that compiles: a pointer or reference to the abstract type
No object of the abstract type is created by a pointer or a reference, and both compile:
#include <iostream>
struct Shape {
virtual double area() const = 0;
virtual ~Shape() = default;
};
struct Circle : Shape {
double area() const override { return 3.0; }
};
int main() {
Circle circle;
Shape* ptr = &circle;
Shape& ref = circle;
std::cout << ptr->area() << ' ' << ref.area() << '\n';
return 0;
}
$ g++ -std=c++20 pointer_ref.cpp -o pointer_ref
$ ./pointer_ref
3 3
This is the normal way abstract bases are used: the interface type appears in pointers and references — including pointer and reference parameters and return types — while the objects themselves are always concrete derived classes. A by-value parameter of the abstract type is barred like any other by-value creation. If code that only holds a Shape* or a Shape& fails with this error, the failing line is somewhere else, where a Shape or a still-abstract derived class is created by value. Search for that creation, not for the pointer or reference declarations.
A neighbor with different wording: a data member of abstract type
A non-static data member of abstract type is also barred, and GCC words it for a field, not a variable:
#include <iostream>
struct Shape {
virtual double area() const = 0;
virtual ~Shape() = default;
};
struct Holder {
Shape shape;
};
int main() {
Holder holder;
std::cout << holder.shape.area() << '\n';
return 0;
}
$ g++ -std=c++20 member.cpp -o member
member.cpp:9:11: error: cannot declare field 'Holder::shape' to be of abstract type 'Shape'
9 | Shape shape;
| ^~~~~
member.cpp:3:8: note: because the following virtual functions are pure within 'Shape':
3 | struct Shape {
| ^~~~~
member.cpp:4:20: note: 'virtual double Shape::area() const'
4 | virtual double area() const = 0;
| ^~~~
The error fires at the member declaration inside Holder, before main runs any code, because creating a Holder would create the Shape member inside it. The fixes follow the same rule: hold a concrete derived object, or hold the base through a pointer or reference member instead of by value.
Neighbors with different wording: return by value and an array
Two more creation forms produce their own messages rather than the variable message. A function that returns an abstract type by value, and an array of an abstract type, each fail differently:
#include <iostream>
struct Shape {
virtual double area() const = 0;
virtual ~Shape() = default;
};
Shape make_shape() {
Shape shape;
return shape;
}
int main() {
Shape shape = make_shape();
std::cout << shape.area() << '\n';
return 0;
}
$ g++ -std=c++20 by_value.cpp -o by_value
by_value.cpp:8:7: error: invalid abstract return type for function 'Shape make_shape()'
8 | Shape make_shape() {
| ^~~~~~~~~~
by_value.cpp:3:8: note: because the following virtual functions are pure within 'Shape':
3 | struct Shape {
| ^~~~~
by_value.cpp:4:20: note: 'virtual double Shape::area() const'
4 | virtual double area() const = 0;
| ^~~~
by_value.cpp: In function 'Shape make_shape()':
by_value.cpp:9:11: error: cannot declare variable 'shape' to be of abstract type 'Shape'
9 | Shape shape;
| ^~~~~
by_value.cpp:10:12: error: cannot allocate an object of abstract type 'Shape'
10 | return shape;
| ^~~~~
by_value.cpp: In function 'int main()':
by_value.cpp:14:29: error: cannot allocate an object of abstract type 'Shape'
14 | Shape shape = make_shape();
| ~~~~~~~~~~^~
by_value.cpp:14:11: error: cannot declare variable 'shape' to be of abstract type 'Shape'
14 | Shape shape = make_shape();
| ^~~~~
Abstract types cannot be used as function return types, and this one program stacks five errors: the invalid return type, two variable errors, and two cannot allocate an object of abstract type errors at the return and the copy. Return a concrete derived object, or a pointer or reference to the base, instead. An array fails in one line, with its own wording:
#include <iostream>
struct Shape {
virtual double area() const = 0;
virtual ~Shape() = default;
};
int main() {
Shape shapes[2];
std::cout << shapes[0].area() << '\n';
return 0;
}
$ g++ -std=c++20 array.cpp -o array
array.cpp: In function 'int main()':
array.cpp:9:19: error: creating array of 'Shape', which is an abstract class type
9 | Shape shapes[2];
| ^
array.cpp:3:8: note: because the following virtual functions are pure within 'Shape':
3 | struct Shape {
| ^~~~~
array.cpp:4:20: note: 'virtual double Shape::area() const'
4 | virtual double area() const = 0;
| ^~~~
An array would create every element, so it is barred for the same reason, and a collection of abstract-base objects is held as pointers or references to concrete objects in practice. In all three neighbor forms the pure-within notes are identical: the class and the open function are the same, only the creation form and its headline message change.
The exact rule: a pure virtual function with no final overrider
A class is abstract when it defines or inherits at least one function whose final overrider is pure virtual. A pure virtual function is a virtual function declared with the pure specifier = 0. A derived class provides the final overrider when it declares a function that overrides the base function: same name, same parameter type list, same cv-qualifiers, and same ref-qualifiers. Until every inherited pure virtual function has a non-pure final overrider in the class, the class is abstract, and no object of it can be created: not as a variable, not as a data member, not as an array element, not as a by-value parameter, and not as a value returned from a function.
Pointers and references to an abstract class can be declared, and an abstract class can appear as a base subobject inside a concrete derived object. Those are the two legal uses, and together they are the intended pattern: an abstract base states the interface, concrete derived classes implement it, and the rest of the code works through base pointers and references. A pure virtual destructor is the one special case in the sources: it makes the class abstract like any other pure virtual function, and it must still be given a definition.
The fixes, ranked
- First, read the note under the error. It names the class that is abstract and quotes each pure virtual function still open in it. That list is the fix list.
- Second, if the variable's type is the base itself, create a concrete derived class that overrides every pure virtual function, and hold the object through a base pointer or reference where the surrounding code is written against the base.
- Third, if the variable's type is a derived class, add an override for each function the note names. One missing override is enough to keep the class abstract.
- Fourth, if the derived class appears to implement the function already, compare signatures character by character against the quoted declaration: name, parameter types,
constand other cv-qualifiers, and ref-qualifiers. A mismatch hides instead of overriding. Fix the signature and mark the functionoverride. - Fifth, add
overrideto every intended override as a standing habit. It converts a silent mismatch into an error at the function, where the defect is.
Which fix for which cause
| Cause | Message | Fix |
|---|---|---|
| Variable of the abstract base itself | cannot declare variable 'shape' to be of abstract type 'Shape', note names the pure function in the base |
Create a concrete derived class; hold it through a base pointer or reference |
| Derived class forgets one of several pure virtual functions | cannot declare variable 'circle' to be of abstract type 'Circle', note names only the missing function |
Add the missing override with the quoted signature |
Derived function has a different signature, for example no const |
Same abstract message; no error at the function line | Match the base signature exactly and mark it override |
Same mismatch written with override |
marked 'override', but does not override at the function, then the abstract message |
Fix the signature named by the first error; both errors clear |
| Pointer or reference to the abstract type | No error | None needed; the object behind it must be a concrete derived class |
| Data member, array element, or return value of abstract type | cannot declare field, creating array of, or invalid abstract return type |
Hold a concrete object, or a pointer or reference to the base, instead of the abstract type by value |
| ## Checklist |
- Read the class named in the error first: it is the variable's type, and it is not always the base class where the pure function was declared.
- Read the pure-within notes next: each quoted declaration is one function that still has no final overrider in that class.
- Count the open functions, not the functions in the base: a function the class already overrides is not in the list.
- If an implementation seems to exist, diff its signature against the quoted declaration, including
const, before assuming the note is wrong. - Check the creation form: variable, field, array, and return-by-value each get their own headline message for the same underlying cause.
- Leave pointers and references to the abstract type alone; find the by-value creation that actually fails.
- Mark intended overrides with
overrideso the next mismatch errors at the function instead of at a variable in another function.
Related guides on this site
- C++ Classes and Objects — the class, object, and member basics behind the types these programs declare.
- Fix C++ Undefined Reference to Vtable for Missing Virtual — the link-time companion error for a virtual function that is declared but never defined.
- Fix C++ Discards Qualifiers Const Method — the const-qualifier companion error, for a non-const method called on a const object.
- C++ Pointers and References — the pointer and reference forms through which abstract base classes are held and used.
Sources
- Abstract class — for a pure virtual function being a virtual function declared with the pure specifier
= 0, for an abstract class being a class that defines or inherits at least one function whose final overrider is pure virtual, for no objects of an abstract class being creatable except base subobjects, for abstract types not being usable as parameter types, function return types, or the type of an explicit conversion, for pointers and references to an abstract class being declarable, and for a pure virtual destructor still requiring a definition. - Virtual function — for a derived function overriding a base virtual function only when the name, parameter type list, cv-qualifiers, and ref-qualifiers match, for a function with the same name but a different parameter list hiding the base function instead of overriding it, and for a call through a base pointer or reference invoking the derived class behavior.
- override specifier — for
overrideensuring that a function is virtual and is overriding a virtual function from a base class, and for the program being ill-formed when that is not true.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.