g++ "error: 'X' was not declared in this scope"
The compiler reached an identifier it has no declaration for. Usually a header was not included, a std:: qualifier is missing, or there is a typo - a genuine source error, not a flaky one.
What this error means
Compilation fails pointing at a name that "was not declared in this scope", often with a "did you mean" suggestion. It is deterministic and reproduces locally with the same compiler.
main.cpp: In function 'int main()':
main.cpp:5:5: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
5 | cout << "hi";
| ^~~~
| std::coutCommon causes
Missing #include or std:: qualifier
Using cout/vector/string without #include <iostream>/<vector>/<string> or without the std:: prefix leaves the name undeclared.
Stricter standard library or compiler version
Newer standard libraries dropped transitive includes (e.g. <cstdint> types no longer pulled in indirectly), so code that compiled on an older toolchain now needs the explicit header.
How to fix it
Include the header and qualify the name
Add the missing header and use the full std:: qualification.
#include <iostream>
int main() { std::cout << "hi"; }Add headers dropped by newer standard libraries
- For
uint32_t/int64_tadd#include <cstdint>. - For
std::runtime_erroradd#include <stdexcept>. - Do not retry - fix the source; this error never passes on a re-run.
How to prevent it
- Include every header you use directly; do not rely on transitive includes.
- Build against the same compiler/standard version locally and in CI.
- Enable
-Walland treat warnings seriously to catch issues early.