Skip to content
Latchkey

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.

g++ output
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::cout

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

C++
#include <iostream>
int main() { std::cout << "hi"; }

Add headers dropped by newer standard libraries

  1. For uint32_t/int64_t add #include <cstdint>.
  2. For std::runtime_error add #include <stdexcept>.
  3. 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 -Wall and treat warnings seriously to catch issues early.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →