was not declared in this scope C/C++ Origin: Application & API Last updated 15 Oct 2026

C++: error: 'x' was not declared in this scope (undeclared identifier)

The compiler met a name it has never seen: it is misspelled, declared later, out of scope, or its header or namespace is missing.

Exact message you may see was not declared in this scopeuse of undeclared identifiererror: 'cout' was not declared in this scope'string' was not declared in this scopedid you mean 'std::
Seen on: C/C++ GCC Clang

Meaning

C++ reads a file top to bottom, so a name must be declared before it is used. The usual causes are a missing #include, a missing std:: prefix, a typo (case matters), or using a variable outside the block where it was declared. GCC's message ends with a "suggested alternative" when it finds a close match.

Common causes

  • Missing #include for the function or type (for example <vector>, <cstring>)
  • Missing std:: or using namespace std
  • Typo or wrong capitalisation in the name
  • Variable declared inside a block and used outside it
  • Function used before its declaration or prototype

⚡ Quick fix

  1. Read GCC's "suggested alternative" hint
  2. Include the right header and qualify the name (std::string)
  3. Declare or prototype the function above its first use

Detailed fix by platform

GCC / Clang

  1. Find which header declares it: man 3 strlen or search cppreference.com for the name
  2. Add the header at the top of the file, not just in another .cpp

C++

  1. Prefer std:: qualification in headers; reserve using namespace std; for small programs

Code examples

Missing header and std:: prefix

cpp
#include <iostream>

int main() {
    vector<int> v{1, 2, 3};          // error: 'vector' was not declared in this scope
    cout << v.size() << "\n";         // error: 'cout' was not declared in this scope
}

// Fix:
// #include <vector>
// std::vector<int> v{1, 2, 3};
// std::cout << v.size() << "\n";

How to diagnose

  1. Name — Spelling and case exactly right?
  2. Header — Is the declaring header included in THIS file?
  3. Scope — Is the name used inside the block where it was declared?

🔧 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):

C2065 — MSVC: error C2065: 'x': undeclared identifier (C/C++)C2065does not name a type — C++: error: 'string' does not name a type (C/C++)does not name a typeexpected ';' before — C/C++: error: expected ';' before 'x' — missing semicolon (C/C++)expected ';' beforeexpected primary-expression before — C++: error: expected primary-expression before ')' token (C/C++)expected primary-expr…expected unqualified-id before — C++: error: expected unqualified-id before 'return' / before ')' token (C/C++)expected unqualified-…implicit declaration of function — C: warning: implicit declaration of function 'foo' [-Wimplicit-function-declaration] (C/C++)implicit declaration …need typename before dependent name — C++: error: need 'typename' before 'T::iterator' because 'T' is a dependent scope (C/C++)need typename before …not a member of std — C++: error: 'optional' in namespace 'std' does not name a template type — wrong -std standard (C/C++)not a member of stdwas not d…

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.