C++
'std::sample' Is Not a Member of 'std' Fix
std::sample lives in one header, <algorithm> - not in <random>, where the std::mt19937 generator the call needs lives and where a sampling function is expected. On this library version a handful of headers rescue it by accident: in the sweep probed here, covering all 106 standard headers in the library's header set, <execution>, <functional>, <regex>, and <valarray> pull the name in on the side, and the textbook combination of <iostream>, <iterator>, <random>, and <vector> does not. This error offers no note at all - no header is named, no include is suggested - so the fix has to come from knowing the name's home. This guide pins down the one failing shape, the four transitive rescues, and the one include that fixes the error by specification.
Written December 2, 2026 · Tested with GCC 13.3.0 · 22 min read
- The error: the include is missing, in a lean file
- The textbook version fails too - with no note to follow
- Neighboring headers: 101 fail the same way, and four rescue it
- The fix
Written December 2, 2026
Tested with GCC 13.3.0
What this guide is
std::sample is a function template defined in the header <algorithm>. It randomly copies a given number of different elements from a source range to a destination range, such that each possible combination has equal probability of appearance, using the given random number generator as the source of randomness. If the requested number is greater than the number of elements in the source range, all elements in the source range are copied. The function has existed since C++17, and its feature-test macro is __cpp_lib_sample (201603L, C++17). The call returns the past-the-end iterator of the destination range. The population iterator only has to meet the input-iterator requirements; the output iterator has to meet the output-iterator requirements, and it must also meet the random-access-iterator requirements when the population iterator does not meet the forward-iterator requirements. The selected elements keep their relative order only when the population iterator is a forward iterator.
Because the function lives in one specific header, 'sample' is not a member of 'std' has one common cause:
- The
#include <algorithm>line is missing, and no other header in the file pulls the name in transitively. For this error, in the contexts probed here, GCC prints no suggestion tail on the error line, no help lines - and no note lines at all. The fix has to come from the name's home header, not from the diagnostic.
This name also has a second cause: std::sample is a C++17 name, so a file that includes <algorithm> and is compiled as C++14 gets the identical error - shown in the versions section below. What makes the error worth separating from its closest relatives is where the name is expected to live. A std::sample call cannot be written without a random number generator, and the generator most often used, std::mt19937, lives in <random> - so <random> is the header already in the file, and in the sweep probed here <random> fails, with the same bare error as every other failing header. The sweep covers every one of the 106 standard headers in this library's header set, compiled as the only header in the program, and the outcome splits only two ways - the one uniform failure, or a rescue - and the rescue set has exactly five entries: <algorithm> itself, <execution>, <functional>, <regex>, and <valarray>.
The error: the include is missing, in a lean file
This program samples 2 elements from a five-element array into an output array, using a small local generator type, but includes only <limits>:
#include <limits>
int main() {
int pop[5] = {1, 2, 3, 4, 5};
int out[3] = {0, 0, 0};
struct Gen {
using result_type = unsigned int;
static constexpr result_type min() { return 0; }
static constexpr result_type max() { return 32767; }
result_type operator()() { state = state * 1103515245u + 12345u; return (state / 65536u) % 32768u; }
unsigned int state = 42;
};
Gen g;
std::sample(pop, pop + 5, out, 2, g);
return 0;
}
$ g++ -std=c++17 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:13:10: error: 'sample' is not a member of 'std'
13 | std::sample(pop, pop + 5, out, 2, g);
| ^~~~~~
That is the whole output: one plain error line, and nothing after it. No suggestion tail rides on the error line itself, there are no note lines, and there are no help lines. The same program with no include at all fails the same way, just as bare, at the same call.
The textbook version fails too - with no note to follow
The version most often written first - sample three elements of a vector into a second vector, with std::mt19937 as the generator - gets the same error, without <algorithm>, and the same silence after it:
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
int main() {
std::vector<int> pop = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> out;
std::mt19937 g{42};
std::sample(pop.begin(), pop.end(), std::back_inserter(out), 3, g);
std::cout << out.size() << "\n";
return 0;
}
$ g++ -std=c++17 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:9:10: error: 'sample' is not a member of 'std'
9 | std::sample(pop.begin(), pop.end(), std::back_inserter(out), 3, g);
| ^~~~~~
What happened: on this library version, none of <iostream>, <iterator>, <random>, or <vector> pulls this name in transitively, so the call fails in a textbook file just as it does in a lean one - and there is no note pointing anywhere. The generator's own header is the trap in this file: <random> declares std::mt19937, the call compiles its generator argument without complaint, and the algorithm itself is still missing. In the sweep probed here, the only headers that rescue std::sample besides <algorithm> itself are <execution>, <functional>, <regex>, and <valarray>, and those rescues are properties of this library version, not rules of the language. A program that uses std::sample includes <algorithm>, whether or not it happens to compile without it.
Neighboring headers: 101 fail the same way, and four rescue it
In a sweep where each of the 106 standard headers in this library's header set was compiled as the only header in a program that samples 2 elements from a five-element array population into an output array with a small local generator type, the outcome splits by compile result two ways. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 101 headers all fail with the plain is not a member of 'std' error line, no suggestion tail on that line, no note lines at all, and no help lines:
<any><array><atomic><barrier><bit><bitset><cassert><ccomplex><cctype><cerrno><cfenv><cfloat><charconv><chrono><cinttypes><ciso646><climits><clocale><cmath><codecvt><compare><complex><concepts><condition_variable><coroutine><csetjmp><csignal><cstdalign><cstdarg><cstdbool><cstddef><cstdint><cstdio><cstdlib><cstring><ctgmath><ctime><cuchar><cwchar><cwctype><deque><exception><expected><filesystem><format><forward_list><fstream><future><initializer_list><iomanip><ios><iosfwd><iostream><istream><iterator><latch><limits><list><locale><map><memory><memory_resource><mutex><new><numbers><numeric><optional><ostream><queue><random><ranges><ratio><scoped_allocator><semaphore><set><shared_mutex><source_location><span><spanstream><sstream><stack><stacktrace><stdexcept><stdfloat><stop_token><streambuf><string><string_view><syncstream><system_error><thread><tuple><type_traits><typeindex><typeinfo><unordered_map><unordered_set><utility><variant><vector><version>
A file with no include at all fails the same way, with the same absence of notes. No failing sole header prints a suggestion tail, a note line, or a help line in this sweep.
A sole <random> file shows the failing shape in full - and <random> is the header this call is most often expected in, since the generator it requires lives there. It is not there:
#include <random>
int main() {
int pop[5] = {1, 2, 3, 4, 5};
int out[3] = {0, 0, 0};
std::mt19937 g{42};
std::sample(pop, pop + 5, out, 2, g);
return 0;
}
$ g++ -std=c++17 v_random.cpp -o v_random_prog
v_random.cpp: In function 'int main()':
v_random.cpp:6:10: error: 'sample' is not a member of 'std'
6 | std::sample(pop, pop + 5, out, 2, g);
| ^~~~~~
And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:
<algorithm><execution><functional><regex><valarray>
Two contrasts with the neighboring sweeps are worth holding on to. First, there is no note class, no suggestion class, and no link-failure shape in this sweep: every failing sole header fails at compile time with the same bare error. Second, the rescue set has no visible math or random shape: the rescues are two execution and functional headers, the regular-expression header, and <valarray> - headers that pull the algorithm header's internals in on the side, which is consistent with a name whose home is the general algorithms header, not the random-number header its generator comes from. <algorithm> - the home header - is on the rescue list by definition, not by transitivity, and the other four rescues are this library version's behavior, not a guarantee.
The fix
Add the header:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
int main() {
std::vector<int> pop = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
std::vector<int> out;
std::mt19937 g{42};
std::sample(pop.begin(), pop.end(), std::back_inserter(out), 3, g);
std::cout << out.size() << ":";
for (int x : out) std::cout << " " << x;
std::cout << "\n";
return 0;
}
$ g++ -std=c++17 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
3: 4 7 10
One include, <algorithm>, declares the function - and with a std::mt19937 seeded with 42 the result is deterministic on this library: 3 elements are written, and they are 4, 7, and 10. Three properties of the documented function are already visible in that one line. The elements are distinct - sampling is without replacement, each element of the population is copied at most once. The count is exact - 3 were asked for and 3 were written, because the population holds 10. And the selected elements appear in the population's own order - 4 before 7 before 10 - because a vector's iterators are forward iterators, and for a forward population the selection is stable.
The function's return value is the past-the-end iterator of the destination range. With an array as the output, that is a plain pointer, and its distance from the start of the array is exactly the number of elements written:
#include <algorithm>
#include <iostream>
#include <random>
int main() {
int pop[5] = {1, 2, 3, 4, 5};
int out[5] = {0, 0, 0, 0, 0};
std::mt19937 g{42};
int* end = std::sample(pop, pop + 5, out, 2, g);
std::cout << (end - out) << ": " << out[0] << " " << out[1] << "\n";
return 0;
}
$ g++ -std=c++17 r_return.cpp -o r_return_prog
$ ./r_return_prog
2: 1 4
The returned iterator sits 2 past the start of the output array - the count that was asked for, and the count that was written - and the two written elements are 1 and 4, again in population order.
The fixed programs: a count past the end, an empty population, and a count of zero
The count is a request, not a promise the population has to be able to meet. Asking for more samples than the population holds copies the whole population, and the empty and zero cases write nothing:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
#include <vector>
int main() {
std::vector<int> small = {1, 2, 3};
std::vector<int> out;
std::mt19937 g{42};
std::sample(small.begin(), small.end(), std::back_inserter(out), 10, g);
std::cout << out.size() << ":";
for (int x : out) std::cout << " " << x;
std::cout << "\n";
std::vector<int> empty;
std::vector<int> out2;
std::mt19937 g2{42};
std::sample(empty.begin(), empty.end(), std::back_inserter(out2), 3, g2);
std::cout << out2.size() << "\n";
std::vector<int> out3;
std::mt19937 g3{42};
std::sample(small.begin(), small.end(), std::back_inserter(out3), 0, g3);
std::cout << out3.size() << "\n";
return 0;
}
$ g++ -std=c++17 k_counts.cpp -o k_counts_prog
$ ./k_counts_prog
3: 1 2 3
0
0
Read in order: asking for 10 samples from the population 1, 2, 3 reports a size of 3, and the output is the whole population, 1 2 3 - the documented rule when the count is greater than the number of elements in the source range. Asking for 3 samples from an empty population reports 0 - there is nothing to copy, and no error. Asking for 0 samples from a non-empty population also reports 0. So the size of the output is the smaller of the count and the population size, in every case probed here, and code that sizes its output from the count alone has to take the population's size into account when the count can exceed it.
A single-pass population: input iterators work, with a condition on the output
The population does not have to be a forward range. The documented requirement on the population iterator is the input-iterator requirements - a single-pass range is a legal population - with one condition attached: when the population is not a forward range, the output iterator must be a random-access iterator. A stream population sampled into a plain array meets that condition, and it runs:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
#include <sstream>
int main() {
std::istringstream in{"1 2 3 4 5 6 7 8"};
int out[8] = {0, 0, 0, 0, 0, 0, 0, 0};
std::mt19937 g{42};
auto end = std::sample(std::istream_iterator<int>{in}, std::istream_iterator<int>{}, out, 3, g);
std::cout << (end - out) << ":";
for (int i = 0; i < 3; ++i) std::cout << " " << out[i];
std::cout << "\n";
return 0;
}
$ g++ -std=c++17 i_input_ra.cpp -o i_input_ra_prog
$ ./i_input_ra_prog
3: 1 7 3
Three elements are written - 1, 7, and 3 - from the single-pass stream of the values 1 through 8, with the same seeded generator. Two things differ from the vector case. The selected elements are not in population order - 7 arrives before 3 - because order preservation is only promised for a forward population; for an input population the implementation may use reservoir sampling, which keeps a running set of candidates instead. And the output here is a raw array, whose iterators are random-access - that is what makes this shape legal.
The same input population with the output the textbook shape reaches for - a back inserter into a vector - does not compile at all. A back inserter is an output iterator, not a random-access iterator, so the condition fails, and the failure is not the not-a-member error: it is a static assertion inside the library header, followed by a no-matching-function error for the library's internal helper:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <random>
#include <sstream>
#include <vector>
int main() {
std::istringstream in{"1 2 3 4 5 6 7 8"};
std::vector<int> out;
std::mt19937 g{42};
std::sample(std::istream_iterator<int>{in}, std::istream_iterator<int>{}, std::back_inserter(out), 3, g);
std::cout << out.size() << "\n";
return 0;
}
$ g++ -std=c++17 i_input_back.cpp -o i_input_back_prog
In file included from /usr/include/c++/13/algorithm:61,
from i_input_back.cpp:1:
/usr/include/c++/13/bits/stl_algo.h: In instantiation of '_SampleIterator std::sample(_PopulationIterator, _PopulationIterator, _SampleIterator, _Distance, _UniformRandomBitGenerator&&) [with _PopulationIterator = istream_iterator<int>; _SampleIterator = back_insert_iterator<vector<int> >; _Distance = int; _UniformRandomBitGenerator = mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>&]':
i_input_back.cpp:11:16: required from here
/usr/include/c++/13/bits/stl_algo.h:5926:74: error: static assertion failed: output range must use a RandomAccessIterator when input range does not meet the ForwardIterator requirements
5926 | is_convertible<__samp_cat, random_access_iterator_tag>>::value,
| ^~~~~
/usr/include/c++/13/bits/stl_algo.h:5926:74: note: 'std::integral_constant<bool, false>::value' evaluates to false
/usr/include/c++/13/bits/stl_algo.h:5935:17: error: no matching function for call to '__sample(std::istream_iterator<int>&, std::istream_iterator<int>&, __pop_cat, std::back_insert_iterator<std::vector<int> >&, __samp_cat, std::__iterator_traits<std::istream_iterator<int>, void>::difference_type&, std::mersenne_twister_engine<long unsigned int, 32, 624, 397, 31, 2567483615, 11, 4294967295, 7, 2636928640, 15, 4022730752, 18, 1812433253>&)'
5935 | __sample(__first, __last, __pop_cat{}, __out, __samp_cat{}, __d,
| ^
/usr/include/c++/13/bits/stl_algo.h:5818:5: note: candidate: 'template<class _InputIterator, class _RandomAccessIterator, class _Size, class _UniformRandomBitGenerator> _RandomAccessIterator std::__sample(_InputIterator, _InputIterator, input_iterator_tag, _RandomAccessIterator, random_access_iterator_tag, _Size, _UniformRandomBitGenerator&&)'
5818 | __sample(_InputIterator __first, _InputIterator __last, input_iterator_tag,
| ^~~~~~~~
/usr/include/c++/13/bits/stl_algo.h:5818:5: note: template argument deduction/substitution failed:
/usr/include/c++/13/bits/stl_algo.h:5935:17: note: cannot convert '__samp_cat()' (type '__samp_cat' {aka 'std::output_iterator_tag'}) to type 'std::random_access_iterator_tag'
5935 | __sample(__first, __last, __pop_cat{}, __out, __samp_cat{}, __d,
| ^
/usr/include/c++/13/bits/stl_algo.h:5845:5: note: candidate: 'template<class _ForwardIterator, class _OutputIterator, class _Cat, class _Size, class _UniformRandomBitGenerator> _OutputIterator std::__sample(_ForwardIterator, _ForwardIterator, forward_iterator_tag, _OutputIterator, _Cat, _Size, _UniformRandomBitGenerator&&)'
5845 | __sample(_ForwardIterator __first, _ForwardIterator __last,
| ^~~~~~~~
/usr/include/c++/13/bits/stl_algo.h:5845:5: note: template argument deduction/substitution failed:
/usr/include/c++/13/bits/stl_algo.h:5935:17: note: cannot convert '__pop_cat()' (type '__pop_cat' {aka 'std::input_iterator_tag'}) to type 'std::forward_iterator_tag'
5935 | __sample(__first, __last, __pop_cat{}, __out, __samp_cat{}, __d,
| ^
The load-bearing line in that output is the static assertion: the output range must use a random-access iterator when the input range does not meet the forward-iterator requirements. So the pairing rule is exact: a forward population accepts any output iterator, including a back inserter; a single-pass population requires a random-access output, such as an array or a vector's own iterators. When a std::sample call over a stream fails, read which error it actually is - this static-assertion shape means the include is already right and the output iterator is the defect.
Versions: C++17 to call it, and no note either way
The call itself needs C++17. The fixed programs in this guide are compiled as C++17 - the function's own standard - and nothing newer is required.
Under C++14 the name is not there at all, even with <algorithm> included - and here this error's silence costs the most, because the output is identical to the missing-include case: the same plain error, and no version note to separate a too-old standard from a missing header:
#include <algorithm>
int main() {
int pop[5] = {1, 2, 3, 4, 5};
int out[3] = {0, 0, 0};
struct Gen {
using result_type = unsigned int;
static constexpr result_type min() { return 0; }
static constexpr result_type max() { return 32767; }
result_type operator()() { state = state * 1103515245u + 12345u; return (state / 65536u) % 32768u; }
unsigned int state = 42;
};
Gen g;
std::sample(pop, pop + 5, out, 2, g);
return 0;
}
$ g++ -std=c++14 c_sample14.cpp -o c_sample14_prog
c_sample14.cpp: In function 'int main()':
c_sample14.cpp:13:10: error: 'sample' is not a member of 'std'
13 | std::sample(pop, pop + 5, out, 2, g);
| ^~~~~~
When the include is already in the file and the error persists, the standard flag is the next thing to check - the diagnostic will not tell you.
The neighboring name: shuffle is not in either
The name std::sample is used alongside does not share the generator's home header either. std::shuffle - the random reordering function, which takes the same kind of generator argument - lives in <algorithm>, and it fails in a file whose only include is <random> with its own plain error and the same absence of a note. It is shown here only as a neighbor, not re-taught:
#include <random>
int main() {
int a[5] = {1, 2, 3, 4, 5};
std::mt19937 g{42};
std::shuffle(a, a + 5, g);
return 0;
}
$ g++ -std=c++17 b_shuffle_random.cpp -o b_shuffle_random_prog
b_shuffle_random.cpp: In function 'int main()':
b_shuffle_random.cpp:5:10: error: 'shuffle' is not a member of 'std'
5 | std::shuffle(a, a + 5, g);
| ^~~~~~~
The neighboring names, fixed
The two names together take the same one added header - <algorithm> covers both, and <random> covers neither algorithm, only the generator they share. The fixed neighbor program reports 5 1 3 4 2 for the shuffle of 1 through 5 with the same seeded generator:
#include <algorithm>
#include <iostream>
#include <random>
int main() {
int a[5] = {1, 2, 3, 4, 5};
std::mt19937 g{42};
std::shuffle(a, a + 5, g);
for (int i = 0; i < 5; ++i) std::cout << a[i] << (i == 4 ? '\n' : ' ');
return 0;
}
$ g++ -std=c++17 b_shuffle_fixed.cpp -o b_shuffle_fixed_prog
$ ./b_shuffle_fixed_prog
5 1 3 4 2
The split is the whole lesson of this family: the algorithms take <algorithm> and the generator takes <random>, the two headers are both needed by a complete sampling program, and a file that has only the generator's header gets the same plain error for either algorithm name.
A different wording - and no argument-dependent rescue for this name
Including <algorithm> is not the only half of the name. This program includes the header, drops the std:: qualifier, and passes plain arrays:
#include <algorithm>
int main() {
int pop[5] = {1, 2, 3, 4, 5};
int out[3] = {0, 0, 0};
struct Gen {
using result_type = unsigned int;
static constexpr result_type min() { return 0; }
static constexpr result_type max() { return 32767; }
result_type operator()() { state = state * 1103515245u + 12345u; return (state / 65536u) % 32768u; }
unsigned int state = 42;
};
Gen g;
sample(pop, pop + 5, out, 2, g);
return 0;
}
$ g++ -std=c++17 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:13:5: error: 'sample' was not declared in this scope; did you mean 'std::sample'?
13 | sample(pop, pop + 5, out, 2, g);
| ^~~~~~
| std::sample
In file included from /usr/include/c++/13/algorithm:61,
from g_unqualified.cpp:1:
/usr/include/c++/13/bits/stl_algo.h:5915:5: note: 'std::sample' declared here
5915 | sample(_PopulationIterator __first, _PopulationIterator __last,
| ^~~~~~
That wording - was not declared in this scope, with did you mean 'std::sample'? on the error line and a note pointing at a declaration - is a different defect with a different fix: the header is present, the qualifier is not. Qualify the name as std::sample; adding includes does not bring the name out of namespace std. Arrays are fundamental types and have no associated namespace, so argument-dependent lookup has nothing to find the name from in this call.
The exact rule
std::sampleis declared by#include <algorithm>, and by no other header by rule. In the sweep probed here, every one of the other 101 headers in the library's header set - among them<random>,<iostream>,<iterator>,<vector>,<numeric>, and<version>- does not declare it, and neither does a file with no include at all.'sample' is not a member of 'std'means the<algorithm>include is missing and nothing else in the file pulled the name in - or, under a standard before C++17, that the name does not exist yet in that standard. In every context probed here, GCC prints no suggestion tail on the error line, no note lines, and no help lines for this name - the missing-include case and the too-old-standard case print identically, and only the file's own include list and standard flag separate them.- In the sweep probed here, the split is two-way: a sole header either fails with that one uniform bare shape, or rescues the program. No probed sole header produces a note class, a suggestion class, or a declared-but-not-defined link-failure shape for this name.
- In the sweep probed here, four headers besides
<algorithm>rescuestd::sampletransitively -<execution>,<functional>,<regex>, and<valarray>- so a file whose only include is one of those compiles and runs the call without<algorithm>. That is this library version's behavior, not a guarantee - in the probed set, no header besides<algorithm>is required to pullstd::samplein. - The count rules are the documented ones: the fixed programs here print
3: 4 7 10for 3 samples from the population 1 through 10 with the seeded generator,3: 1 2 3when 10 samples are asked from a three-element population - the whole population - and0for an empty population and for a count of 0, so the output size is the smaller of the count and the population size. - The return value is the past-the-end iterator of the destination range: the return-value program here prints
2: 1 4, the returned pointer sitting exactly 2 past the start of the output array. - The population iterator needs only the input-iterator requirements, with a condition on the output: the stream program here prints
3: 1 7 3for a single-pass population sampled into an array, while the same population sampled through a back inserter fails on a static assertion - the output range must use a random-access iterator when the input range does not meet the forward-iterator requirements. Order preservation goes with the forward case only: the vector sample prints its elements in population order, and the stream sample does not. std::sampleis a C++17 function: it compiles as C++17 with<algorithm>included, and under C++14 the name itself is absent with the same plain error and no note.std::shuffleis not declared by#include <random>: in the program here, which includes only<random>, it gets its ownis not a member of 'std'error, with no note - and the fixed neighbor program, which includes both<algorithm>and<random>, prints5 1 3 4 2for the shuffle call.'sample' was not declared in this scopefor an unqualifiedsampleuse is a different defect: qualify the name asstd::sample- the include alone does not bring the name out of namespacestd. For this name no argument-dependent-lookup case rescues the unqualified call in the program probed here: array arguments have no associated namespace.
The fixes, ranked
- First, check the include list of the file that names
std::sample. If<algorithm>is not there, add it - that is the fix in the common case, and it holds whether the file failed in a lean context, failed in a textbook file, or happened to compile before through a transitive rescue. - Second, do not wait for a note to confirm the header - for this error there is none. In the contexts probed here the diagnostic is one bare line; take the fix from the name's home header,
<algorithm>, not from the output. - Third, do not reach for
<random>because the call needs a generator. In the sweep probed here,<random>fails forstd::samplewith the same error - the generator and the algorithm live in different headers,std::mt19937in<random>andstd::samplein<algorithm>, and a complete program needs both. - Fourth, do not be reassured by
<iostream>,<iterator>, or<vector>being present - in the sweep here none of them rescues this name at all - and do not remove<algorithm>because a file compiles without it: in the probed set four headers rescue it, and those rescues are not guaranteed. - Fifth, check the standard before changing the code when the include is already right. Under C++14 this name does not exist; under C++17 it exists. The diagnostic will not separate these cases, so read the build's standard flag directly.
- Sixth, if a call over a single-pass population fails with a static assertion about the output range instead of the not-a-member error, fix the output, not the includes: give the call a random-access output iterator, such as an array or a vector's own iterators, instead of a back inserter.
- Seventh, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::sampleas well - and do not look for an argument-dependent-lookup case that makes the unqualified call work over plain arrays, because there is none. - Eighth, if the failing name is the neighboring one, the fix is the same header:
shuffletakes an added<algorithm>too - one header for both algorithm names, and<random>for the generator alone.
Which fix for which cause
#include <algorithm>missing,std::sampleused in a file whose only include is one of the 101 headers listed as failing in the sweep section above, or that includes nothing: the message is'sample' is not a member of 'std', with no suggestion tail, no note lines, and no help lines - add#include <algorithm>.std::sampleused in a textbook file including<iostream>,<iterator>,<random>, and<vector>: the message is the same bare error - add#include <algorithm>.std::sampleused with only<algorithm>,<execution>,<functional>,<regex>, or<valarray>included: no error on this library version - add#include <algorithm>anyway when it is not the include already present, because the other four rescues are not guaranteed.std::sampleused with<algorithm>included under C++14: the message is'sample' is not a member of 'std'with no note - compile as C++17 or later.std::samplecalled over a single-pass input population with a back inserter as the output: the message is a static assertion that the output range must use a random-access iterator when the input range does not meet the forward-iterator requirements - keep<algorithm>and change the output to a random-access iterator.std::shuffleused without<algorithm>in a file whose only include is<random>: the message namesshuffle, with no note - add#include <algorithm>. For that name the header is the same one.sampleused without thestd::qualifier,<algorithm>included, over array arguments: the message is'sample' was not declared in this scope; did you mean 'std::sample'?- qualify the name asstd::sample.
Checklist
- Check the include list of the file that names the call, not just the project. The include must be in that file, or in a header that file includes directly by design.
- Do not read the absence of a note as a different error. For this name the missing-include diagnostic is one bare line in every probed context.
- Do not import the generator's header into this error:
sampleandshuffletake<algorithm>andstd::mt19937takes<random>, so there are two headers in a complete sampling program, one for the algorithms and one for the generator. - Do not assume the textbook includes rescue this name, and do not delete a working
<algorithm>line because a file compiles without it. In the sweep here<iostream>,<iterator>,<random>, and<vector>do not rescuestd::sampleat all, and the four headers that do rescue it do so only transitively. - Match the standard to the use: the call itself needs C++17 and
<algorithm>. - Do not assume the output holds as many elements as the count asked for: asking for 10 from a population of 3 writes 3, and asking from an empty population writes 0. Size from the smaller of the count and the population size.
- Do not assume the selected elements keep the population's order for every population: the vector sample here prints
4 7 10, in order, and the stream sample prints1 7 3, out of order. Order is promised for a forward population only. - Do not pair a single-pass population with a back inserter: that shape fails on a static assertion in the programs here. A single-pass population takes a random-access output.
- After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
3: 4 7 10for the three-sample call,2: 1 4for the return-value call, and5 1 3 4 2for the neighbor shuffle call.
Related guides
- 'std::sort' Is Not a Member of 'std' Fix - the same missing-include shape for the other best-known
<algorithm>name: a file that reaches for the sorting function without the algorithms header gets the same bare error, where this guide's call additionally needs a generator from a second header,<random>, which rescues neither name. - 'std::lerp' Is Not a Member of 'std' Fix - the same missing-include shape in the math family, with the headers swapped in the reader's expectation: its sweep splits into 100 uniform bare failures and five transitive rescues, where this guide's sweep splits into 101 uniform bare failures and four transitive rescues - and the two guides differ in the version floor as well, since
std::lerpneeds C++20 wherestd::sampleneeds only C++17.
Sources
- cppreference,
std::sample— forstd::samplebeing a function template defined in<algorithm>since C++17, randomly copying a given number of different elements from the source range to the destination range such that each possible combination has equal probability of appearance, with the given generator as the source of randomness; for all elements being copied when the count is greater than the number of elements in the source range; for the algorithm being stable only if the population iterator meets the forward-iterator requirements; for the population iterator needing the input-iterator requirements and the output iterator needing the random-access-iterator requirements when the population does not meet the forward-iterator requirements; for the return value being the past-the-end iterator of the destination range; for the complexity being linear in the distance across the source range; for the implementation possibly using selection sampling or reservoir sampling; and for the feature-test macro__cpp_lib_sample(201603L, C++17). - cppreference, Standard library header
<algorithm>— for<algorithm>being the header whose synopsis declaresstd::sampleandstd::shuffle, the home-header fact both the fix and the neighbor sections of this guide rest on.
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.