C++
'std::bitset' Is Not a Member of 'std' Fix
std::bitset lives in one header, <bitset>, and has since C++98 - there is no language-mode half to this error. Miss the header and the name is not a member of std, with a note naming the header to add; include it and the same program compiles in every standard mode. Among the headers probed in this guide's sweep, none rescues the name at all - not even <string>, which <bitset> itself includes for its string conversions: the inclusion runs one way, and <string> does not bring the bitset name back.
Written November 29, 2026 · Tested with GCC 13.3.0 · 16 min read
- The rule underneath: one header, any standard mode
- The failure: no <bitset> include
- The same failure at namespace scope: does not name a template type
- No older-mode shape: <bitset> included compiles at C++98
Written November 29, 2026
Tested with GCC 13.3.0
What this guide is
The error on a std::bitset name means the compiler has no bitset name inside std to find. Inside a function, GCC words it exactly as this guide's title promises: 'bitset' is not a member of 'std'. The cause is a missing include: nothing in the file included <bitset>, the header that declares the name, so the lookup fails at the first use. For this name that is the whole story - there is no second shape. std::bitset dates from C++98, the first standard, so no language mode is too old for it: with the header included, the same program compiles at -std=c++98 and at -std=c++11 alike, as the included-header section below shows. When a std::bitset program fails with this error, the mode is never the fault; the include is.
The wording changes with where the failing name sits. At namespace scope, where the name is used as a type for a variable declaration, GCC reports 'bitset' in namespace 'std' does not name a template type instead - note the word "template": this name is a class template, and the namespace-scope wording says so, where the same fault on a non-template name is worded as "does not name a type". Both wordings appear below; they are one fault - the name is absent - read at two different positions in the file.
This error also carries its remedy with it: GCC attaches a note to the std::bitset error naming the header to add - 'std::bitset' is defined in header '<bitset>'; did you forget to '#include <bitset>'? Read the note before anything else in the cascade, because it is the diagnosis and the fix in one line. And among the headers probed in the sweep below, no neighboring header rescues this name at all - not even <string>, which <bitset> itself includes, because that inclusion runs one way only. The fix stays direct: #include <bitset> in the file itself.
The rule underneath: one header, any standard mode
The std::bitset page defines the type: the class template bitset represents a fixed-size sequence of N bits; bitsets can be manipulated by standard logic operators and converted to and from strings and integers; and for the string representation, the sequence is thought of as having its lowest indexed elements at the right, as in the binary representation of integers. Its one template parameter, N, is the number of bits to allocate storage for - a compile-time size, which matters below. Its member list gives the operations this guide's programs use: operator[] and test both access a specific bit, count returns the number of bits set to true, size returns the number of bits the bitset holds, set sets bits to true or to a given value, and to_string returns a string representation of the data. The page also notes the limit built into the type: if the size of a bit-set is not known at compile time, or its size has to change at run time, a dynamic type such as std::vector<bool> is used instead.
The std: the bitset class template - described there as implementing a constant length bit array - the std::hash<std::bitset> specialization, and the binary logic and stream operators for bitsets. Its synopsis also shows the header's own includes: <bitset> includes <string> and <iosfwd>, because the bitset's string conversions and stream operators need them. So a std::bitset name needs exactly one condition: the header included, so the declarations are in the file. No mode condition stands next to it.
The failure: no <bitset> include
This program uses std::bitset<8> and includes no header at all - deliberately, because a companion header would be its own possible rescue, and the sweep below measures each candidate header on its own instead:
int main() {
std::bitset<8> b;
b.set(1);
return b.test(1) ? 0 : 1;
}
$ g++ -std=c++11 p1_fail_no_include.cpp -o p1_fail_no_include
p1_fail_no_include.cpp: In function 'int main()':
p1_fail_no_include.cpp:2:10: error: 'bitset' is not a member of 'std'
2 | std::bitset<8> b;
| ^~~~~~
p1_fail_no_include.cpp:1:1: note: 'std::bitset' is defined in header '<bitset>'; did you forget to '#include <bitset>'?
+++ |+#include <bitset>
1 | int main() {
p1_fail_no_include.cpp:2:20: error: 'b' was not declared in this scope
2 | std::bitset<8> b;
| ^
Read the note first: it sits directly under the title error and names the header, so this shape diagnoses itself - the include is missing, and the note says which one. The error that follows is the cascade from that one absence: 'b' was not declared in this scope, pointed at the end of the same declaration line, because the declaration failed and the variable does not exist where it is used on the next lines.
The same failure at namespace scope: does not name a template type
The same missing include at namespace scope is worded differently. This program declares the bitset as a file-scope variable, still with no header:
std::bitset<8> g_b;
int main() {
g_b.set(1);
return g_b.test(1) ? 0 : 1;
}
$ g++ -std=c++11 p1b_fail_scope.cpp -o p1b_fail_scope
p1b_fail_scope.cpp:1:6: error: 'bitset' in namespace 'std' does not name a template type
1 | std::bitset<8> g_b;
| ^~~~~~
p1b_fail_scope.cpp:1:1: note: 'std::bitset' is defined in header '<bitset>'; did you forget to '#include <bitset>'?
+++ |+#include <bitset>
1 | std::bitset<8> g_b;
p1b_fail_scope.cpp: In function 'int main()':
p1b_fail_scope.cpp:4:5: error: 'g_b' was not declared in this scope
4 | g_b.set(1);
| ^~~
Same fault, different first line: where the name is used as a type outside a function, GCC says it does not name a template type; where it is used inside a function, GCC says it is not a member of std. The wording is worth recognizing exactly, because "does not name a template type" is this name's namespace-scope form - a non-template name in the same position is reported as "does not name a type", without the word "template". The header note appears here too, unchanged, naming <bitset> as the remedy, and the undeclared-variable error follows inside main exactly as in the function-scope shape. Recognize either wording as the missing-include shape.
No older-mode shape: <bitset> included compiles at C++98
Names that arrived in a later standard have a second failure shape - header included, mode too old - and guides for those names show a #error from inside the library headers as its first diagnostic. This name has no such shape, and the honest way to show that is to compile the included program in the oldest mode: it succeeds. This program includes <bitset>, sets two bits, and prints the bitset and the count of set bits:
#include <bitset>
#include <iostream>
int main() {
std::bitset<8> b;
b.set(1);
b.set(3);
std::cout << b << " " << b.count() << "\n";
return 0;
}
$ g++ -std=c++98 p2_included.cpp -o p2_cpp98
$ ./p2_cpp98
00001010 2
$ g++ -std=c++11 p2_included.cpp -o p2_cpp11
$ ./p2_cpp11
00001010 2
No diagnostics in either mode, and the same output, 00001010 2, from both binaries: bits 1 and 3 are set - counting from the right, as in a binary number - and 2 bits are set in total. So if a std::bitset program shows the title error, do not reach for the compile line - there is no mode in which <bitset> refuses to declare the name, and no "only available from" note exists for it. The missing include is the only shape this error takes, which makes the diagnosis shorter than for the later-standard names: read the header note, add the include, and the error is gone.
The header sweep: no rescue among the headers probed
A missing standard name is sometimes rescued by accident, when another header happens to include the one that declares it. For std::bitset the sweep in this guide finds no rescue at all among the headers probed. Each sweep program uses the bitset with one candidate header as its only include, and no other header - a second include would be its own possible rescue and would spoil the measurement. This program, with <string> in that position and still no <bitset>, fails with exactly the missing-include shape:
#include <string>
int main() {
std::bitset<8> b;
b.set(1);
return b.test(1) ? 0 : 1;
}
$ g++ -std=c++11 sweep_string.cpp -o sweep_string
sweep_string.cpp: In function 'int main()':
sweep_string.cpp:4:10: error: 'bitset' is not a member of 'std'
4 | std::bitset<8> b;
| ^~~~~~
sweep_string.cpp:2:1: note: 'std::bitset' is defined in header '<bitset>'; did you forget to '#include <bitset>'?
1 | #include <string>
+++ |+#include <bitset>
2 |
sweep_string.cpp:4:20: error: 'b' was not declared in this scope
4 | std::bitset<8> b;
| ^
Read what did not change. The title error is still there, word for word: the compiler still says bitset is not a member of std, still attaches the same header note naming <bitset>, and the same undeclared-variable error follows. In the sweep above, no candidate header declared the name, even as a forward declaration - so there is no half-rescued incomplete-type state for this name among the headers probed, unlike names where a probed neighbor declares without defining. The same program was compiled with each of <vector>, <array>, <iostream>, <utility>, <algorithm> and <cstddef> as the only candidate header, at C++11: every one fails with the same title error, the same header note, and the same cascade shape. The <string> result is the one worth pausing on. <bitset> includes <string> - the bitset's string constructor and its to_string() conversion return and consume std::string - so the two headers are genuinely connected. But inclusion is one-directional: <string> does not include <bitset> back, so a file that includes <string> for its strings still has no bitset name, and the to_string() a bitset offers is a member of the bitset, not something <string> provides. So of the seven headers probed in place of <bitset>, all seven leave the title error in place and none produces a working program - among the headers probed, the name simply does not arrive by accident. That makes the repair the direct one: include <bitset> itself in every file that names std::bitset.
The fix: include <bitset>
The header included, and the type is fully usable in any standard mode. This program sets two bits in a bitset, prints the bitset as a string with to_string(), and prints a test() result and the count() of set bits:
#include <bitset>
#include <iostream>
int main() {
std::bitset<8> b;
b.set(1);
b.set(3);
std::cout << b.to_string() << "\n";
std::cout << b.test(3) << " count " << b.count() << "\n";
return 0;
}
$ g++ -std=c++11 p4_fix.cpp -o p4_fix
$ ./p4_fix
00001010
1 count 2
Every name that failed before now resolves. The bitset is built in two steps: set(1) and set(3) set the bits at positions 1 and 3, counting from 0 at the right. to_string() prints the bits most significant first, as in a written binary number, so the string is 00001010 - the ones sit at positions 3 and 1 from the right. test(3) reads the bit at position 3 back and prints 1, because that bit is set, and count() prints 2, the number of set bits. Every printed value here is fully determined by the values in the program, and any other output means the program, not the machine, differs.
Neighboring error: a size that is not a compile-time constant
Once <bitset> is included, a different error appears when the bitset's size is taken from an ordinary variable. The size is the class template's argument - bitset<N>, where N is the number of bits - so it has to be known at compile time. A plain int holding 8 does not qualify, and std::bitset<n> fails like this:
#include <bitset>
int main() {
int n = 8;
std::bitset<n> b;
return b.size() == 8 ? 0 : 1;
}
$ g++ -std=c++11 p5_neighbour_varsize.cpp -o p5_neighbour_varsize
p5_neighbour_varsize.cpp: In function 'int main()':
p5_neighbour_varsize.cpp:5:17: error: the value of 'n' is not usable in a constant expression
5 | std::bitset<n> b;
| ^
p5_neighbour_varsize.cpp:4:9: note: 'int n' is not const
4 | int n = 8;
| ^
p5_neighbour_varsize.cpp:5:18: error: the value of 'n' is not usable in a constant expression
5 | std::bitset<n> b;
| ^
p5_neighbour_varsize.cpp:4:9: note: 'int n' is not const
4 | int n = 8;
| ^
p5_neighbour_varsize.cpp:5:18: note: in template argument for type 'long unsigned int'
5 | std::bitset<n> b;
| ^
p5_neighbour_varsize.cpp:6:14: error: request for member 'size' in 'b', which is of non-class type 'int'
6 | return b.size() == 8 ? 0 : 1;
| ^~~~
The contrast separates the two errors cleanly. This error says nothing about a missing name in std - the include is already right, the type was found, and the failure is the argument given to it: the compiler repeats that the value of n is not usable in a constant expression, with the note 'int n' is not const saying why, and the final non-class type 'int' error is cascade from the failed declaration. The title error names bitset as not a member of std, because the type was never there to be used. The repair here is to give the size in a form the compiler can evaluate while compiling: write the literal, std::bitset<8>, or hold it in a constexpr (or const) variable - constexpr int n = 8; makes the same std::bitset<n> valid. And when the size genuinely is not known until the program runs, no form of the declaration fixes it: a bitset's size is fixed at compile time by design, and the dynamic alternative is std::vector<bool>.
Neighboring error: test() past the last bit throws at run time
The other neighboring fault also keeps the right include, and it is not a compile error at all - the program compiles cleanly and fails only when it runs. test() performs a bounds check, unlike operator[]: asking it for position 8 in a bitset<8>, whose valid positions are 0 through 7, throws std::out_of_range. This program catches the exception and prints it:
#include <bitset>
#include <iostream>
#include <stdexcept>
int main() {
std::bitset<8> b;
b.set(1);
try {
std::cout << b.test(8) << "\n";
} catch (const std::out_of_range& e) {
std::cout << "out_of_range: " << e.what() << "\n";
}
return 0;
}
$ g++ -std=c++11 p6_neighbour_test_range.cpp -o p6_neighbour_test_range
$ ./p6_neighbour_test_range
out_of_range: bitset::test: __position (which is 8) >= _Nb (which is 8)
Nothing in this outcome says a name is missing - the include is right, the bitset was built, the program compiled with no diagnostic, and the failure arrives at run time as an exception: the message states the rule it enforces, that the requested position, 8, is not less than the number of bits, 8. Uncaught, the same throw would terminate the program instead of printing a line. The repair is to keep the position inside the bitset: test only positions below size() - here, 0 through 7 - or, where an index is already known to be valid, read the bit with operator[], which performs no check. The two access forms are a deliberate pair: test() is the checked read, which reports a bad position by throwing, and operator[] is the unchecked read, which assumes the position is right.
Decision summary
- The first errors are
'bitset' is not a member of 'std', with a note under it saying'std::bitset' is defined in header '<bitset>'; did you forget to '#include <bitset>'?: the header is missing - add#include <bitset>, exactly as the note says; every later error in the cascade traces to the absent name. - The first error is
'bitset' in namespace 'std' does not name a template type, with the same header note under it: the same missing include, read at namespace scope where the name is used as a type - add<bitset>the same way. - The file already includes
<bitset>and the program still fails: it is not this error and not a language-mode problem - with the header included, the same program compiles and runs at-std=c++98and at-std=c++11;std::bitsethas no older-mode shape, so read the actual first error instead of changing the compile line. - The error is still the title error even though the file includes some other header: in the sweep above, each of
<string>,<vector>,<array>,<iostream>,<utility>,<algorithm>and<cstddef>in place of<bitset>left the title error in place - among the headers probed, none declares this name by accident, and<string>in particular does not, even though<bitset>includes<string>; add<bitset>itself. - The error instead is
the value of 'n' is not usable in a constant expressionat astd::bitset<n>declaration, with the note'int n' is not const: the type was found and the size is the fault - the bitset's size is a template argument and must be a compile-time constant; write the literal or hold it in aconstexprvariable, and usestd::vector<bool>when the size is only known at run time. - The program instead compiles and then throws
std::out_of_rangefrom atest()call at run time: the include is right and the position is wrong -test()bounds-checks its position, so keep positions belowsize(), or useoperator[]only for positions already known to be valid.
Checklist
- The file includes
<bitset>itself - not a neighboring header in its place; in the sweep above, each of the seven probed headers left the title error in place, so none of them stands in for<bitset>. - No language-mode change is part of this fix:
std::bitsetdates from C++98, and with<bitset>included the program compiles in the oldest mode as well as the newest - if the title error appears, the include is the fault, never the mode. - The first error is read before the cascade: the
'was not declared in this scope'errors disappear when the name error at the top is fixed. - The bitset's size is a compile-time constant - a literal or a
constexprvalue, never a plain variable; a size known only at run time belongs instd::vector<bool>, not in a bitset. - Bits are read with
test()when the position needs checking and withoperator[]only when the position is already known valid:test()throwsstd::out_of_rangefor a position at or pastsize(). - The fixed program compiles, runs, and prints
00001010and1 count 2, in order.
Related guides
- 'std::array' Is Not a Member of 'std' Fix - the same missing-name fault on the other fixed-size sequence type:
std::arrayalso fixes its size at compile time, but it arrived in C++11 and lives in<array>- and<array>in place of<bitset>did not rescue this name in the sweep above. - 'std::vector' Is Not a Member of 'std' Fix - the same missing-name fault on the dynamic sequence container:
std::vectorlives in<vector>, which in place of<bitset>did not rescue this name either - and itsvector<bool>specialization is the dynamic bit sequence to reach for when a bitset's size is not known at compile time. - 'std::stack' Is Not a Member of 'std' Fix - the same missing-name fault with the same mode story:
std::stack, likestd::bitset, dates from C++98 and has no older-mode shape - for both names the missing include is the only shape the error takes.
Sources
- cppreference.com, std::bitset — the type at the center of the error: the class template
bitset, defined in header<bitset>, representing a fixed-size sequence ofNbits that can be manipulated by standard logic operators and converted to and from strings and integers, with its lowest indexed elements at the right, and its member list givingoperator[],test,count,size,setandto_string, the operations the fix and repair programs use; the page also gives the compile-time-size limit, pointing tostd::vector<bool>when the size is not known at compile time. - cppreference.com, Standard library header
— what the header declares: the bitsetclass template, described as implementing a constant length bit array, thestd::hash<std::bitset>specialization, and the binary logic and stream operators, in namespacestd; its synopsis also shows that<bitset>itself includes<string>and<iosfwd>- the declarations a file gets by including<bitset>itself; in the sweep above, no probed neighbor brought them in, and<string>did not bring the bitset name in the other direction. - cppreference.com, std::bitset::set — the member the fix program uses to set individual bits:
set(pos, value)sets the bit at positionpos, counting from 0 from least significant to most significant, to the given value, and throwsstd::out_of_rangeifposdoes not correspond to a valid bit position. - cppreference.com, std::bitset::test — the member the neighboring run-time error exercises:
test(pos)returns the value of the bit at positionpos, counting from 0, performs a bounds check unlikeoperator[], and throwsstd::out_of_rangeifposdoes not correspond to a valid bit position - the rule the neighboring error enforces, and the reason positions passed totest()are kept belowsize().
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.