C++
C++ Does Not Name a Type Fix
The name is spelled almost right, the type exists somewhere in the program, and the compiler still rejects it - because a name in a type position has to resolve, at that exact point, to a declaration of a type: visible, spelled as declared, qualified with the scope it lives in, and complete where a member by value is required. This guide separates the five ways that resolution fails, reads what the diagnostic actually names, and matches each shape to its repair.
Written November 24, 2026 · Tested with GCC 13.3.0 · 10 min read
- The rule underneath: a type name must resolve, at that point, to a complete-enough type
- How to read the diagnostic
- The first failure: the header is missing
- The second failure: the name is misspelled
Written November 24, 2026
Tested with GCC 13.3.0
What this guide is
Every C++ declaration puts some names in type positions: the type of a data member, the type of a variable, a return type. The compiler accepts a name in such a position only if lookup at that point finds a declaration of a type under that exact name. When it finds nothing - or finds declarations, but none of them a type - it stops with "does not name a type", naming the word it could not resolve. The type usually exists: in a header that was never included, further down the same file, inside a namespace, or inside another class. The error is about what is visible and reachable from the line that failed, not about whether the type exists somewhere.
The five forms in this guide each reproduce the failure with a small, complete program, and the name fails to resolve five different ways: the header that declares std::string is missing, the name is misspelled, the type is used above its own definition, the namespace holding the type is not named, and a nested type is used without its enclosing class. In each family the failing program is small, the full compiler output is shown, and the fix makes the one name resolve while keeping the rest of the program as it was. Five neighboring shapes in the traps are not this error: a pointer member needs only a forward declaration, an indirectly included header can hide a missing include on one toolchain, a using-directive brings a namespace's names into reach, a type alias gives a type a second usable name, and a function declaration can name a type that is only forward-declared.
One neighboring failure has its own guide and is not covered here. "Was not declared in this scope" is the sibling error for a name used as a value - a variable or function name inside a function body, where no declaration of any kind is visible. "Does not name a type" is the type-position form of the same underlying problem: it appears at class scope and namespace scope, where a type name is required. A misspelled name inside a function body draws the first error; a misspelled type name in a member declaration draws this one. If the failing line declares a member or a variable's type, this guide is the one that applies.
Build every program in this guide with g++ -std=c++20 -Wall -Wextra. The failing programs are compiled with -c - compile only - because the error is a compile-time error and no program is produced; the fixed programs and the traps are compiled and run, so their output is shown. In the compiler transcripts below, the file names are the bare names shown on the command line.
The rule underneath: a type name must resolve, at that point, to a complete-enough type
Name lookup is the procedure by which a name, when encountered in a program, is associated with the declaration that introduced it. For names other than functions, lookup must produce a single declaration in order for the program to compile. A name can be declared to refer to a type by a class declaration, a union declaration, an enum declaration, a typedef declaration, or a type alias declaration - and if no such declaration is what lookup finds for the spelling used, the name does not name a type, however the program meant it.
Qualification is part of the spelling. A qualified name is a name on the right-hand side of ::, and it is looked up in the scope named on the left: a namespace member is looked up in that namespace, and a class member - including a nested type - is looked up in the scope of that class. The reverse also holds: a name that lives inside a namespace or a class is not found by an unqualified use outside it. Point inside namespace geo is geo::Point, and Color inside struct Palette is Palette::Color; the short forms name nothing at namespace scope, which is the fourth and fifth families below.
Order matters the same way. Lookup runs where the name is written, so a type defined lower in the file is not yet declared at a use above it - the third family. And where the use is a data member held by value, resolution is not the only requirement: the declaration of a non-static class data member is one of the contexts that require the type to be complete, meaning defined, not merely declared. A forward declaration makes a class type known but incomplete; that is enough for a pointer member, and not enough for a member by value - the contrast the first trap draws against the third family.
Library types follow the same rule through their headers. std::string is std::basic_string<char>, defined in the header <string>. Until that header is included, no declaration of the name has been seen, and std::string in a member declaration is a qualified name whose right-hand side lookup fails - the first family. Nothing about the library is special here; a type of your own in a header you forgot to include fails identically.
So the compiler's requirement at a type position is threefold at once: a declaration of a type under the exact spelling used, reachable through the qualification written, at or above the point of use - and, for a member or object by value, a complete type at that point. Each family below breaks exactly one part of that, and each fix repairs exactly that part.
How to read the diagnostic
Every failure in the five families prints the same shape of output. The first family's, in full:
fail_missing_header.cpp:2:10: error: ‘string’ in namespace ‘std’ does not name a type
2 | std::string label;
| ^~~~~~
fail_missing_header.cpp:1:1: note: ‘std::string’ is defined in header ‘<string>’; did you forget to ‘#include <string>’?
+++ |+#include <string>
1 | struct Note {
Read it in order. The first line gives the file and the position - fail_missing_header.cpp:2:10 is line 2, column 10 - and then the error itself, naming the word that failed: 'string' in namespace 'std' does not name a type. The quoted scope matters: the compiler is reporting that inside namespace std, no type by that name was found, which is a different claim from the name not existing anywhere. The next two lines repeat the source line with a caret under the exact name, so the failing spelling can be compared character by character with the declaration it was meant to match. The last block is a note, not a second error: GCC has recognized the shape and names the header that declares the type, <string>, with the include line it suggests. Not every family earns that note - the used-before-defined and nested-type families print the error and the caret only - but where a "did you mean" or an include suggestion appears, it is part of this same single error.
Three practical consequences follow. First, there is exactly one error in each transcript: each program puts exactly one unresolved name in a type position, and the failing member is the only thing the compiler reports. A real class with several members of the same unresolved type can draw one error per member, all with the same cause. Second, the name in quotes is the first piece of evidence - compare it with the declaration: a missing header and a misspelling can print near-identical lines, and the difference is whether the quoted spelling matches the real type's name. Third, the position is the second piece of evidence: the error is reported where the name is used, and the repair is made there or above it - an include, a reordering, a qualification - never below the failing line.
The first failure: the header is missing
A struct with a std::string member and no include of any kind:
struct Note {
std::string label;
};
int main() {
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_missing_header.o fail_missing_header.cpp
fail_missing_header.cpp:2:10: error: ‘string’ in namespace ‘std’ does not name a type
2 | std::string label;
| ^~~~~~
fail_missing_header.cpp:1:1: note: ‘std::string’ is defined in header ‘<string>’; did you forget to ‘#include <string>’?
+++ |+#include <string>
1 | struct Note {
No declaration of string in namespace std has been seen anywhere in this file, so the qualified lookup fails and the member has no type. The compiler's note says the rest: std::string is defined in the header <string>, and the include was forgotten. This is the most common form of the error with library types, and it often arrives indirectly - code moved from a file that included the header, or copied from an example that showed only the class. Note what did not happen: the compiler did not complain about std itself, or about the struct, or about main. One name failed, at one position, and that is the whole error.
The fix adds the one include:
#include <iostream>
#include <string>
struct Note {
std::string label;
};
int main() {
Note note{"ready"};
std::cout << note.label << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_missing_header fix_missing_header.cpp
$ ./fix_missing_header
ready
The output is ready, the label the program stored in the member. The durable habit behind this fix is to include what you use: a header for each library type a file names directly, whether or not the file happens to compile without it today - the second trap shows why "it compiles" is not evidence that the include is present.
The second failure: the name is misspelled
The header is present, and the member's type is std::strng:
#include <string>
struct Note {
std::strng label;
};
int main() {
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_misspelled_name.o fail_misspelled_name.cpp
fail_misspelled_name.cpp:4:10: error: ‘strng’ in namespace ‘std’ does not name a type; did you mean ‘string’?
4 | std::strng label;
| ^~~~~
| string
The include did its work - namespace std is full of declared types - but none of them is spelled strng, so the lookup still fails, and the error adds its own diagnosis: did you mean 'string'?. GCC offers that suggestion when the failed spelling is close to a declared name, and here it is exactly right. This family is the one to suspect when the include is visibly present and the error persists: read the quoted name in the diagnostic against the intended type letter by letter, because the eye slides over a missing letter in code it has already read once - the compiler's caret sits under the misspelling, and the suggestion names the correction.
The fix corrects the spelling:
#include <iostream>
#include <string>
struct Note {
std::string label;
};
int main() {
Note note{"ready"};
std::cout << note.label << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_misspelled_name fix_misspelled_name.cpp
$ ./fix_misspelled_name
ready
The output is ready. One word changed; nothing else in the program was wrong. When the "did you mean" suggestion names a type you do not recognise, treat that as information too - the compiler is reporting the nearest declared type, and a surprising suggestion usually means the intended type's declaration is missing as well, which is the first family wearing this one's clothes.
The third failure: the type is used before it is defined
A struct whose member is a Helper that is defined only below it:
struct Board {
Helper helper;
};
struct Helper {
int value;
};
int main() {
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_used_before_defined.o fail_used_before_defined.cpp
fail_used_before_defined.cpp:2:5: error: ‘Helper’ does not name a type
2 | Helper helper;
| ^~~~~~
At the line where Board is defined, Helper has not been declared anywhere yet - its definition sits further down, and lookup does not look ahead. The name therefore names nothing at that point, even though the same file defines it a few lines later. This is the form the error takes when code is reorganized: a helper type extracted, a block moved, a new member added of a type that the file defines in a later section. The failing program's main deliberately does nothing with the member - using board.helper in the failing version would add further errors about the incomplete board, all downstream of this one.
The fix defines Helper before Board:
#include <iostream>
struct Helper {
int value;
};
struct Board {
Helper helper;
};
int main() {
Board board{{42}};
std::cout << board.helper.value << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_used_before_defined fix_used_before_defined.cpp
$ ./fix_used_before_defined
42
The output is 42, the value stored through the member. Order was the whole problem: a by-value member needs the member's type complete at the point of the enclosing definition, and completeness requires the definition itself to have appeared. Where reordering is awkward - two types that refer to each other, for instance - the shape usually changes instead: a pointer or reference member needs only a forward declaration, which is the first trap.
The fourth failure: the namespace is not named
A Point defined inside namespace geo, and a member that uses the bare name:
namespace geo {
struct Point {
int x;
int y;
};
} // namespace geo
struct Canvas {
Point origin;
};
int main() {
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_namespace_not_named.o fail_namespace_not_named.cpp
fail_namespace_not_named.cpp:11:5: error: ‘Point’ does not name a type; did you mean ‘int’?
11 | Point origin;
| ^~~~~
| int
Point is declared - inside geo. The member declaration is at namespace scope outside geo, and an unqualified Point there looks up names in the enclosing scopes, where no Point exists; the type inside the namespace is not found, because it is not named Point at this scope - it is named geo::Point. The compiler's did you mean 'int'? suggestion is a distraction here: it is the nearest name it can offer at this scope, not a hint about the real type. This family appears when a type moves into a namespace, when code is copied out of a namespace block, and when a using-directive that used to bring the names in is removed.
The fix qualifies the member's type:
#include <iostream>
namespace geo {
struct Point {
int x;
int y;
};
} // namespace geo
struct Canvas {
geo::Point origin;
};
int main() {
Canvas canvas{{3, 4}};
std::cout << canvas.origin.x << ',' << canvas.origin.y << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_namespace_not_named fix_namespace_not_named.cpp
$ ./fix_namespace_not_named
3,4
The output is 3,4, the coordinates stored in the member. Qualification is the durable repair because it states, at the use, which scope the type lives in, and it keeps working if another Point is later declared elsewhere. The third trap shows the alternative - a using-directive that makes the bare name work - and why it is the weaker default: it changes lookup for every name in the file, not just this one.
The fifth failure: a nested type is used without its class
An enum defined inside struct Palette, and a member in another struct that uses the bare name:
struct Palette {
enum class Color { Red, Blue };
};
struct Swatch {
Color color = Color::Red;
};
int main() {
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_nested_type.o fail_nested_type.cpp
fail_nested_type.cpp:6:5: error: ‘Color’ does not name a type
6 | Color color = Color::Red;
| ^~~~~
Color is a member of Palette - a type declared in the scope of that class - so at namespace scope there is no Color, and both uses in the member declaration fail together as one error: the member's type and its initializer name the same unresolved word. Nested types are easy to strand this way because inside Palette itself the short name works, and member functions of Palette use it unqualified; the name only breaks when another class, or namespace-scope code, reaches for it. Scoped enums make the shape more common, since an enum class is almost always declared inside the class it belongs to and used everywhere else.
The fix qualifies both uses with the enclosing class:
#include <iostream>
struct Palette {
enum class Color { Red, Blue };
};
struct Swatch {
Palette::Color color = Palette::Color::Red;
};
int main() {
Swatch swatch{};
std::cout << (swatch.color == Palette::Color::Red ? "red" : "blue") << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_nested_type fix_nested_type.cpp
$ ./fix_nested_type
red
The output is red, the enumerator the member was initialized with. Both spellings changed - Palette::Color for the type and Palette::Color::Red for the value - because a scoped enum's enumerators live inside the enum's scope as well: qualifying only the type and leaving Color::Red in the initializer would fail again on the same line, this time with 'Color' has not been declared.
The fixes in reach order
- The header is missing: include the header that declares the type - the compiler's note often names it directly.
- The name is misspelled: correct the spelling to the declared name; the "did you mean" suggestion usually prints it.
- The type is used before it is defined: move the definition above the use, or change the member to a pointer or reference over a forward declaration.
- The namespace is not named: qualify the type with its namespace,
geo::Point. - A nested type is unqualified: qualify it with its enclosing class,
Palette::Color- and qualify its enumerators the same way. - In every case: the repair makes lookup at the failing line find a declaration of a type, complete where the member is held by value.
Traps
- A forward declaration is enough for a pointer member - the direct contrast with the third family. Here
Helperis declared, used as a pointer member, and defined afterward:
#include <iostream>
struct Helper;
struct Board {
Helper* helper;
};
struct Helper {
int value;
};
int main() {
Helper helper{42};
Board board{&helper};
std::cout << board.helper->value << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o probe_pointer_member probe_pointer_member.cpp
$ ./probe_pointer_member
42
42, read through the pointer. A pointer member does not require a complete type: the pointer has a known size whatever Helper turns out to contain, so the incomplete type suffices at Board's definition, and the definition of Helper is only needed where the program reaches through the pointer, by which point it has appeared. The same program with the member held by value instead of through a pointer is the third family verbatim - the difference between compiling and not compiling is exactly the *.
- An indirectly included header can hide the first family completely - on this toolchain. The first family's program, with <iostream> added and still no <string>:#include <iostream>
struct Note {
std::string label;
};
int main() {
Note note{"ready"};
std::cout << note.label << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o probe_transitive_include probe_transitive_include.cpp
$ ./probe_transitive_include
ready
The output is ready, and no include of <string> appears anywhere in the file. With this standard library, <iostream> pulls in declarations that bring std::string with them, so the lookup succeeds. That is a property of this library's internal arrangement, not a promise: another library version, or another toolchain, may organize its headers differently and the same file fails. The direct include is the fix that travels - a file that names std::string includes <string>, and treats any help from other headers as luck, not structure.
- A using-directive makes the bare name work. The fourth family's shape, with one line added before Canvas:
#include <iostream>
namespace geo {
struct Point {
int x;
int y;
};
} // namespace geo
using namespace geo;
struct Canvas {
Point origin;
};
int main() {
Canvas canvas{{3, 4}};
std::cout << canvas.origin.x << ',' << canvas.origin.y << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o probe_using_directive probe_using_directive.cpp
$ ./probe_using_directive
3,4
The output is 3,4. The directive makes the names in geo visible to unqualified lookup in this file, so Point resolves. It compiles, and for a file that uses many names from one namespace it can be the right call - but it applies to every name in the file, and two namespaces with a Point each turn the next use ambiguous. Qualifying the one member, as the fourth family's fix does, changes nothing else's lookup.
- An alias names the type from where it is declared. With <string> included, a type alias gives std::string a second name, and the member uses it:
#include <iostream>
#include <string>
using Label = std::string;
struct Note {
Label label;
};
int main() {
Note note{"ready"};
std::cout << note.label << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o probe_alias probe_alias.cpp
$ ./probe_alias
ready
The output is ready. A type alias declaration is one of the declarations that makes a name refer to a type, so Label in the type position resolves exactly as std::string would. The alias is declared in this file, above the use - an alias written in a header that is never included, or below the struct, fails in the first or third family's way, because an alias obeys the same visibility and order rules as every other declaration.
- A function declaration can name a type that is only forward-declared. Here make_helper is declared while Helper is incomplete, and defined after Helper is:
#include <iostream>
struct Helper;
Helper* make_helper();
struct Helper {
int value;
};
Helper* make_helper() {
static Helper helper{42};
return &helper;
}
int main() {
std::cout << make_helper()->value << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o probe_function_declaration probe_function_declaration.cpp
$ ./probe_function_declaration
42
The output is 42. A declaration needs the names it mentions to be declared, not complete: returning a pointer to an incomplete type is fine, because nothing in the declaration needs the type's size or members. The definition and the call come after the definition of Helper, where completeness is required and present. This is the shape that lets headers declare functions over types they only forward-declare - and it is the declaration half of the first trap: pointers and declarations tolerate incompleteness, members and objects by value do not.
Checklist
- The diagnostic read as a lookup report: file and position of the use, the quoted name that failed, the scope the compiler searched - and any "did you mean" or include note read as part of the same single error.
- The quoted spelling compared character by character with the declaration of the intended type.
- A declaration of that type visible above the failing line: the header included, the definition not sitting below the use.
- The qualification matching the scope the type lives in: namespace named for a namespace member, enclosing class named for a nested type, enumerators qualified with both.
- For a member held by value, the type complete at that point - a definition, not a forward declaration; a pointer or reference member is the shape that needs only the declaration.
- The fixed program run, and its output checked against what the program was meant to do.
Related guides on this site
- Marked Override Does Not Override Fix
- 'std::unique' Is Not a Member of 'std' Fix
- 'std::search_n' Is Not a Member of 'std' Fix
- 'std::replace' Is Not a Member of 'std' Fix
Sources
- cppreference, Name lookup - for name lookup being the procedure that associates a name with the declaration that introduced it, and for lookup of a non-function name having to produce a single declaration in order for the program to compile.
- cppreference, Qualified name lookup - for a qualified name being looked up in the scope named on the left of
::, including namespace members in their namespace and class members, nested types among them, in the scope of their class. - cppreference, Type - for a name being declared to refer to a type by a class, union, enum, typedef, or type alias declaration, for a class type declared but not defined being an incomplete type, and for the declaration of a non-static data member and the definition of an object being contexts that require a complete type.
- cppreference, std::basic_string - for
std::stringbeingstd::basic_string<char>and for both being defined in the header<string>.
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.