C++

'std::shuffle' Is Not a Member of 'std' Fix

std::shuffle lives in one header, <algorithm> - not in <random>, where the std::mt19937 generator the call needs lives and where a shuffling 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>, <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

By the end of this guide
  • 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::shuffle is a function template defined in the header <algorithm>. It reorders the elements in a range so that each possible permutation of those elements has equal probability of appearance, using the given generator object as the source of randomness. The generator's type must meet the uniform-random-bit-generator requirements, the range's iterators must meet the random-access-iterator requirements, and the elements must be swappable. The function has existed since C++11, returns nothing, and performs exactly one fewer swap than the number of elements in the range.

Because the function lives in one specific header, 'shuffle' 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::shuffle is a C++11 name, so a file that includes <algorithm> and is compiled as C++98 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::shuffle 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 shuffles a five-element array using a small local generator type, but includes only <limits>:

C++
#include <limits>
int main() {
    int a[5] = {1, 2, 3, 4, 5};
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::shuffle(a, a + 5, g);
    return 0;
}
Terminal
$ g++ -std=c++17 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:12:10: error: 'shuffle' is not a member of 'std'
   12 |     std::shuffle(a, a + 5, 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 - shuffle a vector with std::mt19937 as the generator - gets the same error, without <algorithm>, and the same silence after it:

C++
#include <iostream>
#include <random>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::mt19937 g{42};
    std::shuffle(v.begin(), v.end(), g);
    for (int x : v) std::cout << x << " ";
    std::cout << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:7:10: error: 'shuffle' is not a member of 'std'
    7 |     std::shuffle(v.begin(), v.end(), g);
      |          ^~~~~~~

What happened: on this library version, none of <iostream>, <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::shuffle 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::shuffle 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 shuffles a five-element 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:

C++
#include <random>
int main() {
    int a[5] = {1, 2, 3, 4, 5};
    std::mt19937 g{42};
    std::shuffle(a, a + 5, g);
    return 0;
}
Terminal
$ g++ -std=c++17 v_random.cpp -o v_random_prog
v_random.cpp: In function 'int main()':
v_random.cpp:5:10: error: 'shuffle' is not a member of 'std'
    5 |     std::shuffle(a, a + 5, 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:

C++
#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::mt19937 g{42};
    std::shuffle(v.begin(), v.end(), g);
    for (std::size_t i = 0; i < v.size(); ++i) std::cout << v[i] << (i + 1 == v.size() ? '\n' : ' ');
    return 0;
}
Terminal
$ g++ -std=c++17 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
2 7 8 1 6 10 9 3 4 5

One include, <algorithm>, declares the function - and with a std::mt19937 seeded with 42 the result is deterministic on this library: the population 1 through 10 comes out as 2 7 8 1 6 10 9 3 4 5. The order is the whole result, because this function returns nothing and works in place: the range the caller passed is the range that changed.

That the output is a genuine shuffle, and not lost or duplicated elements, is checkable from the result itself - sorting the shuffled range restores the input, and a second shuffle with the same seed reproduces the order exactly:

C++
#include <algorithm>
#include <iostream>
#include <random>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int> orig = v;
    std::mt19937 g{42};
    std::shuffle(v.begin(), v.end(), g);
    for (std::size_t i = 0; i < v.size(); ++i) std::cout << v[i] << (i + 1 == v.size() ? '\n' : ' ');
    std::sort(v.begin(), v.end());
    std::cout << (v == orig ? "permutation" : "not permutation") << "\n";
    std::vector<int> a = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::vector<int> b = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    std::mt19937 g1{42};
    std::mt19937 g2{42};
    std::shuffle(a.begin(), a.end(), g1);
    std::shuffle(b.begin(), b.end(), g2);
    std::cout << (a == b ? "same seed same order" : "different") << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 r_perm.cpp -o r_perm_prog
$ ./r_perm_prog
2 7 8 1 6 10 9 3 4 5
permutation
same seed same order

Read in order: the shuffled order is the same 2 7 8 1 6 10 9 3 4 5 as the fixed program; sorting it restores the original population, so the shuffle is a permutation - every element present exactly once; and two generators seeded alike shuffle two identical populations into the same order, so a seeded shuffle is reproducible, which is what makes a shuffled test case debuggable. The standard does not promise this exact order on other libraries - the implementation is not dictated by the standard - only that each permutation is equally likely.

There is no return value to capture

Unlike its neighbor std::sample, std::shuffle returns nothing. Trying to store its result is its own error, different from the not-a-member error:

C++
#include <algorithm>
#include <random>
int main() {
    int a[5] = {1, 2, 3, 4, 5};
    std::mt19937 g{42};
    auto x = std::shuffle(a, a + 5, g);
    return 0;
}
Terminal
$ g++ -std=c++17 r_void.cpp -o r_void_prog
r_void.cpp: In function 'int main()':
r_void.cpp:6:10: error: deduced type 'void' for 'x' is incomplete
    6 |     auto x = std::shuffle(a, a + 5, g);
      |          ^

So code that needs the shuffled order reads the range itself after the call; there is no returned iterator to test, no count to read, and no end marker to compare against.

The edge ranges: empty and one element

Neither short range is an error. A shuffle over an empty range does nothing, and a shuffle over one element leaves it in place - the swap count, one fewer than the element count, is zero in both cases:

C++
#include <algorithm>
#include <iostream>
#include <random>
int main() {
    int a[1] = {7};
    std::mt19937 g{42};
    std::shuffle(a, a, g);
    std::shuffle(a, a + 1, g);
    std::cout << a[0] << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 k_empty.cpp -o k_empty_prog
$ ./k_empty_prog
7

Versions: C++11 to call it, and no note either way

The call itself needs C++11. The fixed programs in this guide are compiled as C++17, and nothing newer than the function's own standard is required: the same call, with a std::mt19937, compiles under C++11:

C++
#include <algorithm>
#include <random>
int main() {
    int a[5] = {1, 2, 3, 4, 5};
    std::mt19937 g{42};
    std::shuffle(a, a + 5, g);
    return 0;
}
Command
$ g++ -std=c++11 c_shuffle11.cpp -o c_shuffle11_prog
$ ./c_shuffle11_prog

Under C++98 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. This probe's generator is written in C++98-shaped code, so the shuffle line is the only failure:

C++
#include <algorithm>
int main() {
    int a[5] = {1, 2, 3, 4, 5};
struct Gen {
    typedef unsigned int result_type;
    static result_type min() { return 0; }
    static result_type max() { return 32767; }
    Gen() : state(42) {}
    result_type operator()() { state = state * 1103515245u + 12345u; return (state / 65536u) % 32768u; }
    unsigned int state;
};
    Gen g;
    std::shuffle(a, a + 5, g);
    return 0;
}
Terminal
$ g++ -std=c++98 c_shuffle98.cpp -o c_shuffle98_prog
c_shuffle98.cpp: In function 'int main()':
c_shuffle98.cpp:13:10: error: 'shuffle' is not a member of 'std'
   13 |     std::shuffle(a, a + 5, 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 predecessor name runs the other way. std::random_shuffle, the pre-C++11 shuffling function, was deprecated in C++14 and removed in C++17 - but on this library version it still compiles under C++17, with a deprecation warning that names the replacement:

C++
#include <algorithm>
#include <random>
int main() {
    int a[5] = {1, 2, 3, 4, 5};
    std::random_shuffle(a, a + 5);
    return 0;
}
Terminal
$ g++ -std=c++17 d_random_shuffle17.cpp -o d_random_shuffle17_prog
d_random_shuffle17.cpp: In function 'int main()':
d_random_shuffle17.cpp:5:24: warning: 'void std::random_shuffle(_RAIter, _RAIter) [with _RAIter = int*]' is deprecated: use 'std::shuffle' instead [-Wdeprecated-declarations]
    5 |     std::random_shuffle(a, a + 5);
      |     ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:61,
                 from d_random_shuffle17.cpp:1:
/usr/include/c++/13/bits/stl_algo.h:4581:5: note: declared here
 4581 |     random_shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last)
      |     ^~~~~~~~~~~~~~

That is this library version keeping a removed name available, not a reason to use it: the warning's advice is the documented direction, and code that shuffles takes std::shuffle with a generator.

The neighboring name: sample is not in either

The name std::shuffle is used alongside does not share the generator's home header either. std::sample - the random selection 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:

C++
#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;
}
Terminal
$ g++ -std=c++17 b_sample_random.cpp -o b_sample_random_prog
b_sample_random.cpp: In function 'int main()':
b_sample_random.cpp:6:10: error: 'sample' is not a member of 'std'
    6 |     std::sample(pop, pop + 5, out, 2, 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 2: 1 4 for two samples from the population 1 through 5 with the same seeded generator - the returned iterator sits 2 past the start of the output array, and the two written elements are 1 and 4:

C++
#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;
}
Terminal
$ g++ -std=c++17 b_sample_fixed.cpp -o b_sample_fixed_prog
$ ./b_sample_fixed_prog
2: 1 4

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 shuffling program, and a file that has only the generator's header gets the same plain error for either algorithm name.

A different wording - and an argument-dependent rescue that depends on the generator

Including <algorithm> is not the only half of the name, but for this call the unqualified form splits on what generator is passed. With a std::mt19937 argument, the unqualified call compiles and runs:

C++
#include <algorithm>
#include <random>
int main() {
    int a[5] = {1, 2, 3, 4, 5};
    std::mt19937 g{42};
    shuffle(a, a + 5, g);
    return 0;
}
Command
$ g++ -std=c++17 g_unqualified.cpp -o g_unqualified_prog
$ ./g_unqualified_prog

That compile is argument-dependent lookup at work, not a rule to rely on: the generator argument is a std type, so its associated namespace is std, and lookup finds std::shuffle through it. Swap the standard generator for a small local generator type, and the same unqualified call fails:

C++
#include <algorithm>
int main() {
    int a[5] = {1, 2, 3, 4, 5};
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;
    shuffle(a, a + 5, g);
    return 0;
}
Terminal
$ g++ -std=c++17 g_unqualified_gen.cpp -o g_unqualified_gen_prog
g_unqualified_gen.cpp: In function 'int main()':
g_unqualified_gen.cpp:12:5: error: 'shuffle' was not declared in this scope; did you mean 'std::shuffle'?
   12 |     shuffle(a, a + 5, g);
      |     ^~~~~~~
      |     std::shuffle
In file included from /usr/include/c++/13/algorithm:61,
                 from g_unqualified_gen.cpp:1:
/usr/include/c++/13/bits/stl_algo.h:3742:5: note: 'std::shuffle' declared here
 3742 |     shuffle(_RandomAccessIterator __first, _RandomAccessIterator __last,
      |     ^~~~~~~

That wording - was not declared in this scope, with did you mean 'std::shuffle'? 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::shuffle; adding includes does not bring the name out of namespace std. Arrays are fundamental types and have no associated namespace, and a local generator type's associated namespace is not std either, so in that call argument-dependent lookup has nothing to find the name from - while a std generator argument supplies the association by accident. Write the qualifier either way.

The exact rule

  • std::shuffle is 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>, <vector>, <numeric>, and <version> - does not declare it, and neither does a file with no include at all.
  • 'shuffle' 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++11, 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> rescue std::shuffle transitively - <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 pull std::shuffle in.
  • The result rules are the documented ones, as the fixed programs here show: the fixed program prints 2 7 8 1 6 10 9 3 4 5 for the population 1 through 10 with the seeded generator - a permutation of the input, since sorting it restores the input - and two shuffles with the same seed produce the same order. The exact order is this library's; the standard promises only that each permutation is equally likely.
  • The function returns nothing: capturing its result fails with deduced type 'void' for 'x' is incomplete. The shuffled range itself is the output, read in place after the call.
  • The edge ranges are ordinary: shuffling an empty range or a one-element range runs cleanly - the one-element program here prints 7, its element unmoved - because the swap count is one fewer than the element count.
  • std::shuffle is a C++11 function: it compiles as C++11 with <algorithm> included, and under C++98 the name itself is absent with the same plain error and no note. Its predecessor std::random_shuffle was removed in C++17 but still compiles on this library version under C++17 with a deprecation warning - a library extension to leave alone, not a fallback.
  • The range's iterators must meet the random-access-iterator requirements and the generator's type must meet the uniform-random-bit-generator requirements; the elements must be swappable.
  • std::sample is not declared by #include <random>: in the program here, which includes only <random>, it gets its own is not a member of 'std' error, with no note - and the fixed neighbor program, which includes both <algorithm> and <random>, prints 2: 1 4 for the sample call.
  • 'shuffle' was not declared in this scope for an unqualified shuffle use is a different defect: qualify the name as std::shuffle - the include alone does not bring the name out of namespace std. For this name the unqualified call's fate depends on its arguments: over plain arrays, a std::mt19937 generator argument rescues it through argument-dependent lookup and a local generator type does not, as the two programs above show - and standard container iterators are std types too, so a call over them can be rescued through the iterator arguments even with a local generator. Qualify the name either way.

The fixes, ranked

  • First, check the include list of the file that names std::shuffle. 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 for std::shuffle with the same error - the generator and the algorithm live in different headers, std::mt19937 in <random> and std::shuffle in <algorithm>, and a complete program needs both.
  • Fourth, do not be reassured by <iostream> or <vector> being present - in the sweep here neither 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++98 this name does not exist; under C++11 it exists. The diagnostic will not separate these cases, so read the build's standard flag directly.
  • Sixth, do not fall back to std::random_shuffle when std::shuffle is missing: on this library it still compiles, with a deprecation warning, but it is a removed name whose randomness source is weaker - add <algorithm> and call std::shuffle with a generator instead.
  • Seventh, if the error says was not declared in this scope instead, fix the name's qualifier to std::shuffle as well - and do not rely on a std generator argument to rescue the unqualified call through argument-dependent lookup, because a local generator type removes that rescue.
  • Eighth, if the failing name is the neighboring one, the fix is the same header: sample takes 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::shuffle used 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 'shuffle' is not a member of 'std', with no suggestion tail, no note lines, and no help lines - add #include <algorithm>.
  • std::shuffle used in a textbook file including <iostream>, <random>, and <vector>: the message is the same bare error - add #include <algorithm>.
  • std::shuffle used 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::shuffle used with <algorithm> included under C++98: the message is 'shuffle' is not a member of 'std' with no note - compile as C++11 or later.
  • The result of std::shuffle captured in a variable: the message is deduced type 'void' for 'x' is incomplete - drop the capture; the function returns nothing and the shuffled range is the result.
  • std::sample used without <algorithm> in a file whose only include is <random>: the message names sample, with no note - add #include <algorithm>. For that name the header is the same one.
  • shuffle used without the std:: qualifier, <algorithm> included, over array arguments with a local generator type: the message is 'shuffle' was not declared in this scope; did you mean 'std::shuffle'? - qualify the name as std::shuffle. With a std::mt19937 argument the same unqualified call compiles through argument-dependent lookup - qualify it anyway, so the call does not depend on the generator's type.

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: shuffle and sample take <algorithm> and std::mt19937 takes <random>, so there are two headers in a complete shuffling 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>, <random>, and <vector> do not rescue std::shuffle at all, and the four headers that do rescue it do so only transitively.
  • Match the standard to the use: the call itself needs C++11 and <algorithm>.
  • Do not look for a return value: there is none. Read the shuffled range in place, and check a shuffle by its contents - sorting a copy restores the input if the shuffle was a true permutation.
  • Do not treat a seeded result as portable: the same seed gives the same order on this library, and the standard does not promise that order on another library.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 2 7 8 1 6 10 9 3 4 5 for the ten-element shuffle, permutation and same seed same order for the checks, and 2: 1 4 for the neighbor sample call.
  • 'std::sample' Is Not a Member of 'std' Fix - the same missing-include shape for the other random-selection name in this family, with the same five-entry rescue set: its sweep splits into 101 uniform bare failures and four transitive rescues, exactly as this guide's sweep does - and the two calls differ in what they hand back, since std::sample returns the past-the-end iterator of its output range where std::shuffle returns nothing and works in place.
  • 'std::sort' Is Not a Member of 'std' Fix - the same missing-include shape for the best-known <algorithm> name: a file that reaches for the sorting function without the algorithms header gets the same bare error, and the two functions pair naturally - sorting a shuffled range is how the permutation check in this guide restores the input.

Sources

  • cppreference, std::shuffle — for std::shuffle being a function template defined in <algorithm> since C++11, reordering the elements in a range so that each possible permutation has equal probability of appearance, with the given generator object as the source of randomness; for the generator's type needing to meet the uniform-random-bit-generator requirements, with undefined behavior when it does not, or when its result type is not convertible to the range's difference type (until C++20); for the range's iterators needing the random-access-iterator requirements and the elements needing to be swappable; for the return type being void; for the complexity being exactly one fewer swap than the number of elements in the range; for the implementation not being dictated by the standard, so the same generator may give different results with different standard library implementations; and for std::random_shuffle being deprecated in C++14 and removed in C++17, with std::shuffle as its preferred replacement.
  • cppreference, Standard library header <algorithm> — for <algorithm> being the header whose synopsis declares std::shuffle and std::sample, the home-header fact both the fix and the neighbor sections of this guide rest on.

Keep Exploring