CppPilot

Compiler diagnostic Visual Studio C++

C2027 use of undefined type

The type is declared but not defined at that point: the compiler knows its name, not its size or its members. A forward declaration such as class Widget; permits declarations using pointers or references, but creating an object, accessing its members, or taking its size requires a complete type.

Applies toMSVC on Windows; code and debugger concepts apply to MSBuild and CMake
ExamplesFocused excerpts; required context is stated alongside each example

Visual Studio property-page paths describe MSBuild projects. For CMake, configure the corresponding target or preset. Menu names can vary by Visual Studio version; use the cited reference for your toolset.

Build output
error C2027: use of undefined type 'Widget'

Causes & corrections

Find the case that matches your build. Each example shows what changes and why.

  1. The code uses a member of a forward-declared class.

    Include the header that defines the class in the .cpp file that calls its members, creates it, or copies it.

    Problem

    class Widget;void run(Widget* p) { p->draw(); }

    Correction

    #include "widget.h"void run(Widget* p) { p->draw(); }
    Note 1The compiler must see the class members to compile a member call.
    VideoFilmed in Visual Studio: this case fixed by hand, then with CppPilot. English narration, with subtitles in English, French, German, Italian and Spanish.
    Transcript

    C2027, use of undefined type: fixed by hand, then with CppPilot. job.h only forward-declares Printer.

    job.cpp calls printer.Print, but never includes printer.h: the compiler knows the name, not the members, and reports C2027. Open job.cpp, and include printer.h, which defines Printer.

    Build again: Printer is complete in job.cpp, and the build succeeds. Now the same error, fixed by CppPilot.

    After the failed build, select the error in the Error List, and click Fix with CppPilot. Click Fix on the C2027 card.

    The AI sees that job.cpp only knows Printer by its forward declaration, and includes printer.h. CppPilot rebuilds the project to check the fix: the error is resolved.

    CppPilot explains and fixes C++ build errors in Visual Studio.

  2. Two headers include each other.

    Keep the forward declaration in one header, move the code that uses the members to the .cpp file, and include the full header there.

    Problem

    // owner.hclass Widget;struct Owner {    Widget* widget;    void draw() { widget->draw(); }};

    Correction

    // owner.hclass Widget;struct Owner { Widget* widget; void draw(); };// owner.cpp#include "owner.h"#include "widget.h"void Owner::draw() { widget->draw(); }
    Note 2Move code needing the complete type out of the header that only forward-declares it.
  3. A std::unique_ptr member points to an incomplete type.

    With the pimpl idiom, the destructor must be defined where the type is complete: declare ~Owner(); in the header and define Owner::~Owner() = default; in the .cpp file, after the include.

    Problem

    // owner.h, with <memory> includedclass Impl;struct Owner {    std::unique_ptr<Impl> impl;    ~Owner() = default;};

    Correction

    // owner.h: declare ~Owner();// owner.cpp#include "owner.h"#include "impl.h"Owner::~Owner() = default;
    Note 3Define the destructor where Impl is complete.
  4. A class contains an object of its own type.

    A class cannot contain itself by value: use a pointer or a container such as std::vector.

    Problem

    struct Node { Node next; };

    Correction

    #include <memory>struct Node { std::unique_ptr<Node> next; };
    Note 4Indirection gives the node a finite size; choose ownership semantics that fit the structure.

Sources and scope

Official references

These links document the compiler, build system, or debugger behavior discussed here. Use the version selector on Microsoft Learn for your toolset. Example blocks are focused excerpts; required headers and build settings are stated alongside them.

In your workspace

Investigate C2027 with CppPilot

CppPilot gives the selected AI the diagnostic, relevant code, and project settings for the active configuration. Choose Fix to request a correction, then check the result with a rebuild.

Use your own Codex or Claude account, or optional CppPilot Cloud access. Setup instructions

Visual Studio Marketplace