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.
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:: 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
#includefor the function or type (for example<vector>,<cstring>) - Missing
std::orusing 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
- Read GCC's "suggested alternative" hint
- Include the right header and qualify the name (
std::string) - Declare or prototype the function above its first use
Detailed fix by platform
GCC / Clang
- Find which header declares it:
man 3 strlenor search cppreference.com for the name - Add the header at the top of the file, not just in another .cpp
C++
- Prefer
std::qualification in headers; reserveusing namespace std;for small programs
Code examples
Missing header and std:: prefix
#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
- Name — Spelling and case exactly right?
- Header — Is the declaring header included in THIS file?
- 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:
Similar errors
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- double free or corruption C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)
- format expects argument of type C: warning: format '%d' expects argument of type 'int', but argument 2 has type 'long int' [-Wformat=]
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