does not name a type C/C++ Origin: Application & API OSI layer 7 · Application Last updated 15 Oct 2026

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.

Exact message you may see does not name a typeerror: 'string' does not name a typeerror: 'vector' does not name a typehas not been declaredunknown type name
Seen on: C/C++ GCC Clang

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

  1. Add the header and std:: prefix
  2. Forward-declare the class or reorder definitions
  3. Move statements into a function such as main()

Detailed fix by platform

C++

  1. #include <string> then std::string name;
  2. Check that code like x = 5; is inside a function, not at file level

Code examples

Statement outside a function

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

  1. Scope — Is this at file or class level?
  2. Header — Is the type's header included?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

'this' was not captured — C++: error: 'this' was not captured for this lambda function (C/C++)'this' was not captur…call of overloaded is ambiguous — C++: error: call of overloaded 'f(int)' is ambiguous (C/C++)call of overloaded is…cannot be implicitly captured — Clang: error: variable 'x' cannot be implicitly captured in a lambda with no capture-default specified (C/C++)cannot be implicitly …cannot bind non-const lvalue reference — C++: error: cannot bind non-const lvalue reference of type 'T&' to an rvalue of type 'T' (C/C++)cannot bind non-const…case label does not reduce to an integer constant — C/C++: error: case label does not reduce to an integer constant (C/C++)case label does not r…comparison of integers of different signs — C/C++: warning: comparison of integer expressions of different signedness: 'int' and 'size_t' [-Wsign-compare] (C/C++)comparison of integer…conflicting types — C: error: conflicting types for 'foo' (C/C++)conflicting typescrosses initialization — C++: error: jump to case label crosses initialization of 'int x' (C/C++)crosses initializationdoes not …

Explore the map of this error Full error map →

🧠 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.