C++: error: 'string' does not name a type
Where the compiler expected a type, it found a name it does not know as one — a missing header, namespace, or class declared later.
does not name a typeerror: 'string' does not name a typeerror: 'vector' does not name a typehas not been declaredunknown type name Meaning
At global or class scope, the compiler only accepts declarations. An unknown word there is reported as "does not name a type". It usually means the type's header is missing, std:: is missing, a typo, or the type is defined further down.
Common causes
- Missing
#include <string>/<vector>and similar - Missing
std::prefix at namespace or class scope - Typo or wrong letter case
- Class used before its definition (needs a forward declaration)
- Statement placed at global scope (code outside any function)
⚡ Quick fix
- Add the header and
std::prefix - Forward-declare the class or reorder definitions
- Move statements into a function such as
main()
Detailed fix by platform
C++
#include <string>thenstd::string name;- Check that code like
x = 5;is inside a function, not at file level
Code examples
Statement outside a function
#include <string>
std::string name;
name = "Ada"; // error: 'name' does not name a type (assignment at global scope)
int main() {
name = "Ada"; // OK
}How to diagnose
- Scope — Is this at file or class level?
- Header — Is the type's header included?
- Order — Is the type defined above this line?
🔧 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
- call of overloaded is ambiguous C++: error: call of overloaded 'f(int)' is ambiguous
- comparison of integers of different signs C/C++: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' [-Wsign-compare]
- declared and not used Go: declared and not used: x / "fmt" imported and not used
Most viewed in C/C++
- AddressSanitizer: heap-use-after-free C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
- Bus error C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file
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