C++
Was Not Declared in This Scope Fix
The compiler found the name in your code and no declaration for it where you used it. That single sentence covers several different mistakes, and the repair depends on which one you made: correct a spelling, move a declaration earlier, qualify a standard name, name the namespace or object that owns the name, or move a declaration out of the block that hides it. This guide separates those causes, reads what the diagnostic and its suggestions actually report, and matches each shape to its fix.
Written November 25, 2026 · Tested with GCC 13.3.0 · 6 min read
- The rule underneath: a name is visible only after its declaration, in its scope
- How to read the diagnostic
- The first failure: a misspelled name
- The second failure: use before declaration
Written November 25, 2026
Tested with GCC 13.3.0
What this guide is
When the compiler reads a name, it looks for a declaration of that name that is visible at that point, in that scope. If there is none, it stops with "was not declared in this scope" and names the name it could not find. The name in the message is the whole clue: ask where that name was meant to be declared, and why that declaration is not visible here, and the answer picks the fix.
The causes in this guide each reproduce the failure with a small, complete program: a misspelled name, a variable used before its declaration, a standard-library name written without std::, a name that lives in a namespace, a member name used without an object, and a name declared inside a block and used after the block ends. A function called before its definition, fixed with a forward declaration, and a missing header, whose wording is close but different, are covered alongside them. In each family the failing program is small, the compiler output is shown, and the fix makes the intended declaration visible at the point of use - by correcting the spelling, moving or adding a declaration, qualifying the name, or widening the scope it lives in.
Build every program in this guide with g++ -std=c++20 -Wall -Wextra. The failing programs are compiled with -c - compile only - because the error prevents any program from being produced; the fixed programs are compiled and run, so their output is shown.
The rule underneath: a name is visible only after its declaration, in its scope
A name lookup associates a name in a program with the declaration that introduced it. That lookup searches the scopes enclosing the point of use, and a declaration only counts from the point where it appears: a name is visible after its declaration, not before it, and only inside the scope that declaration belongs to - a block, a namespace, a class, or the global scope.
That single rule produces every family below. A misspelling names a different name, one that was never declared anywhere. A use above its declaration arrives too early. An unqualified cout searches the current scope and finds nothing, because the declaration lives in namespace std. A namespace member and a class member are declared, but not in the scope the unqualified use searches. A block variable's scope ends at the closing brace, so a use after it is a use with no declaration.
Three consequences follow. First, the compiler's suggestions are worth reading: when a similarly spelled or qualified declaration exists, this compiler names it - "did you mean" lines point at the declaration it did find, and that line often is the fix. Second, a missing header is a different wording, not this one: with no #include <iostream>, std::cout fails as "is not a member of std", shown below, because the qualified lookup itself finds no std member at all. Third, adding a declaration is not always the fix - if the name was meant to be a name already declared elsewhere, the fix is to reach that declaration, by spelling, qualification, or scope, not to declare a second, unrelated name.
How to read the diagnostic
Every failure in the main families prints the same shape of output. The third family's, in full:
fail_missing_std.cpp: In function ‘int main()’:
fail_missing_std.cpp:3:5: error: ‘cout’ was not declared in this scope; did you mean ‘std::cout’?
3 | cout << 42 << endl;
| ^~~~
| std::cout
In file included from fail_missing_std.cpp:1:
/usr/include/c++/13/iostream:63:18: note: ‘std::cout’ declared here
63 | extern ostream cout; ///< Linked to standard output
| ^~~~
Read it in order. The first line gives the file and position, then the error itself: the name in quotes is the name that has no visible declaration. The caret sits under that name in your line. Below it, a suggestion line may name a declaration the compiler did find - here std::cout, with a note giving the header line where it is declared. When a suggestion appears, it is evidence about where the intended declaration lives; when none appears, as in the use-before-declaration and block-scope families, the declaration usually does not exist yet at that point at all.
The first failure: a misspelled name
A program that declares count and uses coutn:
#include <iostream>
int main() {
int count = 5;
std::cout << coutn << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_typo.o fail_typo.cpp
fail_typo.cpp: In function ‘int main()’:
fail_typo.cpp:4:18: error: ‘coutn’ was not declared in this scope; did you mean ‘count’?
4 | std::cout << coutn << '\n';
| ^~~~~
| count
fail_typo.cpp:3:9: warning: unused variable ‘count’ [-Wunused-variable]
3 | int count = 5;
| ^~~~~
Two clues confirm a spelling mistake rather than a missing declaration. The "did you mean" line names count, the declaration one transposition away, and the warning reports that count is unused - because the only use spells it differently. A renamed variable whose uses were not all updated produces the same pair of clues, with the new spelling in the suggestion.
The fix corrects the spelling:
#include <iostream>
int main() {
int count = 5;
std::cout << count << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_typo fix_typo.cpp
$ ./fix_typo
5
The output is 5. No new declaration was needed - the declaration was already there, under the spelling the use did not match.
The second failure: use before declaration
A program that uses total on the line before it is declared:
#include <iostream>
int main() {
std::cout << total << '\n';
int total = 42;
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_use_before.o fail_use_before.cpp
fail_use_before.cpp: In function ‘int main()’:
fail_use_before.cpp:3:18: error: ‘total’ was not declared in this scope
3 | std::cout << total << '\n';
| ^~~~~
There is no suggestion line here, and the declaration visibly exists - one line too late. A name becomes visible at its declaration and stays visible to the end of its scope; it is not visible above that line, even in the same function. Moving the use down would also work, but the declaration is what other code is built around, so the usual repair is to move the declaration up.
The fix declares total before its use:
#include <iostream>
int main() {
int total = 42;
std::cout << total << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_use_before fix_use_before.cpp
$ ./fix_use_before
42
The output is 42. The same shape at file scope is a function called before its definition. Here show is called in main and defined after it:
#include <iostream>
int main() {
show();
return 0;
}
void show() {
std::cout << "ready" << '\n';
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_func_before.o fail_func_before.cpp
fail_func_before.cpp: In function ‘int main()’:
fail_func_before.cpp:3:5: error: ‘show’ was not declared in this scope
3 | show();
| ^~~~
A function does not need its full definition before the call - a declaration is enough, with the definition following anywhere in the file. The fix adds that forward declaration:
#include <iostream>
void show();
int main() {
show();
return 0;
}
void show() {
std::cout << "ready" << '\n';
}
$ g++ -std=c++20 -Wall -Wextra -o fix_func_before fix_func_before.cpp
$ ./fix_func_before
ready
The output is ready. One declaration line before main makes the later definition visible at the call.
The third failure: a standard name without std::
A program that includes <iostream> and writes cout and endl unqualified:
#include <iostream>
int main() {
cout << 42 << endl;
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_missing_std.o fail_missing_std.cpp
fail_missing_std.cpp: In function ‘int main()’:
fail_missing_std.cpp:3:5: error: ‘cout’ was not declared in this scope; did you mean ‘std::cout’?
3 | cout << 42 << endl;
| ^~~~
| std::cout
In file included from fail_missing_std.cpp:1:
/usr/include/c++/13/iostream:63:18: note: ‘std::cout’ declared here
63 | extern ostream cout; ///< Linked to standard output
| ^~~~
fail_missing_std.cpp:3:19: error: ‘endl’ was not declared in this scope; did you mean ‘std::endl’?
3 | cout << 42 << endl;
| ^~~~
| std::endl
In file included from /usr/include/c++/13/iostream:41:
/usr/include/c++/13/ostream:735:5: note: ‘std::endl’ declared here
735 | endl(basic_ostream<_CharT, _Traits>& __os)
| ^~~~
The header is included, so the declarations exist - in namespace std. An unqualified use does not search inside std, so each name fails separately, and each suggestion spells the qualified form. The header is not the problem here; the qualification is.
The fix qualifies both names:
#include <iostream>
int main() {
std::cout << 42 << std::endl;
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_missing_std fix_missing_std.cpp
$ ./fix_missing_std
42
The output is 42. A using declaration is the other legitimate repair for a name used repeatedly - the traps show one - but qualifying each use is the direct fix, and it keeps every standard name visibly standard.
The neighboring failure is a missing header. With no include at all, the wording changes:
int main() {
std::cout << 42 << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_missing_include.o fail_missing_include.cpp
fail_missing_include.cpp: In function ‘int main()’:
fail_missing_include.cpp:2:10: error: ‘cout’ is not a member of ‘std’
2 | std::cout << 42 << '\n';
| ^~~~
fail_missing_include.cpp:1:1: note: ‘std::cout’ is defined in header ‘<iostream>’; did you forget to ‘#include <iostream>’?
+++ |+#include <iostream>
1 | int main() {
That note is the fix: add the header it names. Adding #include <iostream> makes the program above compile and print 42, exactly as in the qualified fix. Read the two wordings as a fork - "was not declared in this scope" with a std:: suggestion means qualify the name; "is not a member of std" with an include suggestion means add the header. For header failures in general, see C++ Header Not Found Fix.
The fourth failure: a name inside a namespace
A program that declares value inside namespace app and uses it unqualified in main:
#include <iostream>
namespace app {
int value = 7;
}
int main() {
std::cout << value << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_namespace.o fail_namespace.cpp
fail_namespace.cpp: In function ‘int main()’:
fail_namespace.cpp:6:18: error: ‘value’ was not declared in this scope; did you mean ‘app::value’?
6 | std::cout << value << '\n';
| ^~~~~
| app::value
fail_namespace.cpp:3:9: note: ‘app::value’ declared here
3 | int value = 7;
| ^~~~~
This is the third family one level down: the declaration lives in a named namespace instead of std, and the suggestion spells its qualified name and points at its line. Your own namespaces behave exactly like the standard one here - a member is reached by its qualified name, by a use inside the namespace, or by a using declaration.
The fix qualifies the name:
#include <iostream>
namespace app {
int value = 7;
}
int main() {
std::cout << app::value << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_namespace fix_namespace.cpp
$ ./fix_namespace
7
The output is 7. The declaration did not move; the use learned where it lives.
The fifth failure: a member name without an object
A program that declares a member limit in a struct and uses the bare member name in main:
#include <iostream>
struct Config {
int limit = 3;
};
int main() {
std::cout << limit << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_class_scope.o fail_class_scope.cpp
fail_class_scope.cpp: In function ‘int main()’:
fail_class_scope.cpp:6:18: error: ‘limit’ was not declared in this scope
6 | std::cout << limit << '\n';
| ^~~~~
A member belongs to class scope: it names part of an object, not a free-standing variable, so outside a member function there is no bare limit to find - and no suggestion, because the compiler cannot know which object's limit was meant. Inside a member function of Config the bare name would be found, as that object's own member; in main the object has to be named.
The fix names an object and reaches its member through it:
#include <iostream>
struct Config {
int limit = 3;
};
int main() {
Config c;
std::cout << c.limit << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_class_scope fix_class_scope.cpp
$ ./fix_class_scope
3
The output is 3, the member's initializer value on the object c. A name that exists only as a member failing this way is easy to confuse with a missing type or a missing declaration - for the neighboring wording, see C++ Does Not Name a Type Fix.
The sixth failure: a name declared inside a block
A program that declares inner inside an if block and uses it after the block:
#include <iostream>
int main() {
if (true) {
int inner = 9;
std::cout << inner << '\n';
}
std::cout << inner << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -c -o fail_block_scope.o fail_block_scope.cpp
fail_block_scope.cpp: In function ‘int main()’:
fail_block_scope.cpp:7:18: error: ‘inner’ was not declared in this scope
7 | std::cout << inner << '\n';
| ^~~~~
The first use, inside the block, is fine - the error is only at the second use, on line 7, after the closing brace. A block variable's scope ends at that brace, and the variable itself is destroyed there too, so there is nothing for the later use to refer to. The same shape appears with a variable declared inside a loop body or a bare pair of braces and used after them.
The fix declares the variable in the enclosing scope, before the block:
#include <iostream>
int main() {
int inner = 9;
if (true) {
std::cout << inner << '\n';
}
std::cout << inner << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o fix_block_scope fix_block_scope.cpp
$ ./fix_block_scope
9
9
The output is 9 twice, once inside the block and once after it. Moving the declaration out is a lifetime decision as well as a scope fix - the variable now lives to the end of main - so the alternative repair, when nothing after the block needs the value itself, is to move the later work inside the block instead. When a call then fails because the declaration is visible but no overload fits, that is a different error: see C++ No Matching Function for Call Fix.
The fixes in reach order
- Read the name in the error and the "did you mean" line, if any: a near spelling is a typo or a rename that missed a use - correct the use to the declared spelling.
- No suggestion, and a declaration exists lower in the same scope: the use is too early - move the declaration above the first use, or add a forward declaration for a function defined later.
- The suggestion is a
std::name and the header is included: qualify the name -std::cout,std::endl,std::string- or bring that one name in with a using declaration. - The wording is "is not a member of
std" and the note names a header: add the#includeit names - the header, not the qualification, is missing. - The suggestion is a name in your own namespace: qualify the use with that namespace, or use the name from inside the namespace.
- The name is a member: name an object and use
object.member- a member has no bare existence outside its class. - The declaration is inside braces and the use is outside them: move the declaration to the enclosing scope, or move the use inside the block.
- In every case: fix the first reported name, recompile, and only then read any errors that remain.
Traps
- A using declaration brings a name into the current scope legitimately - the unqualified use after it is not this error. Here
coutis used bare after being brought in by name:
#include <iostream>
int main() {
using std::cout;
cout << 42 << '\n';
return 0;
}
$ g++ -std=c++20 -Wall -Wextra -o trap_using trap_using.cpp
$ ./trap_using
42
42. The using declaration is itself a declaration of the name in this block scope, so lookup finds it. It names one name at a time, which is what makes it a precise alternative to qualifying every use; a using directive for a whole namespace does the same for every name in it and is the broader, blunter version of the same move.
- A declaration in an enclosing scope stays visible inside a nested block - only the reverse direction fails. The fixed block-scope program above is the proof: inner, declared in main's scope, is used both inside the if block and after it, and prints 9 twice. Lookup searches outward from the point of use, so outer names are found from inside; inner names are never found from outside, because their scope has already ended.Checklist
- The name in the error matched against the declarations in the file: spelling identical, character for character.
- Every use placed after its declaration in the same scope - or, for a function, after a forward declaration.
- Standard names either qualified with
std::or brought in by a using declaration, with the header that declares them included. - A missing-header wording ("is not a member of") fixed with the
#includeits note names, not with qualification. - Namespace members reached by their qualified name, a use inside the namespace, or a using declaration.
- Class members reached through an object - no bare member name outside a member function.
- No use of a block variable after its closing brace: the declaration moved out, or the use moved in.
- The fixed program run, and its output checked against what the program was meant to do.
Related guides on this site
- Fix C++ "'cout' was not declared in this scope" — Missing <iostream>, Missing std::, and the using namespace std Trade-Off
- 'std::unique' Is Not a Member of 'std' Fix
- 'std::search_n' Is Not a Member of 'std' Fix
- 'std::replace' Is Not a Member of 'std' Fix
Sources
- cppreference, Name lookup - for name lookup associating a name with its declaration, and for qualified lookup finding
coutin namespacestd. - cppreference, Scope - for block, namespace, and class scopes, and for a name being visible after the point of its declaration.
- GCC, Name lookup - for this compiler's lookup of names at the point of use and its guidance to qualify or declare a name the lookup cannot find.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.