C++

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

std::generate lives in one header, <algorithm> - and in the sweep probed here, covering all 106 standard headers in this library's header set, only 4 other headers pull it in on the side: <execution>, <functional>, <regex>, and <valarray>. Its error splits in two: most headers fail with one plain line, but 36 of them - the textbook <vector> and <iostream> among them - attach a suggestion that names the wrong function, 'negate'. This guide pins down the failing shapes, the 4 transitive rescues, what the generator is called and how many times, and the include that fixes it by rule.

Written December 2, 2026 · Tested with GCC 13.3.0 · 30 min read

By the end of this guide
  • The error: the include is missing
  • The textbook version fails too - and its diagnostic offers the wrong name
  • Neighboring headers: 65 fail plainly, 36 fail with the wrong suggestion, and 4 rescue it by accident
  • The fix

Written December 2, 2026

Tested with GCC 13.3.0

What this guide is

std::generate is a function template defined in the header <algorithm>. It assigns the result of successive evaluations of a generator - a function, function object, or lambda called with no arguments - to each element of a range, in order: the first element gets the first call's result, the second element the second call's, and so on. The classic call returns nothing. The iterators need only meet the forward-iterator requirements, and the call makes exactly N evaluations of the generator and N assignments over a range of N elements. The classic call predates C++11, has an execution-policy overload since C++17, and has been usable in constant expressions since C++20. Its counted companion std::generate_n lives in the same header and fills only the first count elements, returning the iterator just past the last one filled.

Because the function lives in <algorithm> - not in a container's header, and not in <numeric> - 'generate' is not a member of 'std' is the missing include, and naming that cause is the first step:

  • 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, that second clause rarely saves anyone: in the sweep below, only 4 of the other 105 headers in the library's header set pull the name in on the side, so most files without <algorithm> reach this error.

And for this name the missing include is the only cause. Some names in this family hide a second cause behind the same wording - a standard floor the name starts at - but std::generate predates C++11, so a file that includes <algorithm> compiles this call at every standard level probed here, C++98 included, as the versions section below shows. The pieces layered on top do have floors - the execution-policy overload needs C++17 and constant-expression use needs C++20 - but those floors belong to the added pieces, not to the classic call, and neither produces this error line for the classic call.

This name's diagnostic has a trap its plainer neighbors lack. In 36 of the 101 failing sole-header contexts probed here - the textbook file among them - the error line carries a suggestion, and the suggestion is wrong: it proposes negate, a unary negation functor from a different corner of the library, which cannot fill a range. A file a reader actually has - <vector> for the container, <iostream> for printing - fails with that wrong suggestion attached. 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 three ways - the plain failure, the failure with the wrong suggestion, or a rescue - and the rescue set has 5 entries: <algorithm> itself and 4 transitive rescues.

The error: the include is missing

This program fills a five-element array with 1, 2, 3, 4, 5 from a counter generator, but includes only <limits>:

C++
#include <limits>
int main() {
    int a[5] = {0, 0, 0, 0, 0};
    int n = 0;
    std::generate(a, a + 5, [&n] { return ++n; });
    return a[0] == 1 && a[4] == 5 ? 0 : 1;
}
Output
-std=c++17 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:5:10: error: 'generate' is not a member of 'std'
    5 |     std::generate(a, a + 5, [&n] { return ++n; });
      |          ^~~~~~~~

Read it in full: one plain error line - no suggestion tail rides on it, there are no note lines, and there are no help lines. Nothing in the diagnostic names <algorithm> or points anywhere at all. The same program with no include at all fails the same way:

Output
-std=c++17 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:4:10: error: 'generate' is not a member of 'std'
    4 |     std::generate(a, a + 5, [&n] { return ++n; });
      |          ^~~~~~~~

The textbook version fails too - and its diagnostic offers the wrong name

The version most often written first - fill a vector with a counting sequence and print it - produces this error as well, without <algorithm>:

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v(5);
    int n = 0;
    std::generate(v.begin(), v.end(), [&n] { return ++n; });
    for (int x : v) std::cout << x << " ";
    std::cout << "\n";
    return 0;
}
Output
-std=c++17 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:6:10: error: 'generate' is not a member of 'std'; did you mean 'negate'?
    6 |     std::generate(v.begin(), v.end(), [&n] { return ++n; });
      |          ^~~~~~~~
      |          negate

It fails, and this time the failure carries a suggestion - did you mean 'negate'?, with negate repeated under the caret. Follow it and the program still does not fill anything: std::negate is a function object that negates its one argument, not a generator-driven range fill, and swapping the name in trades this error for type errors downstream. The suggestion appears because the textbook includes happen to declare negate on the side; the compiler's nearest-name guess is a spelling neighbor, not a semantic one. A sole <vector> file fails with the same suggested wording:

Output
-std=c++17 v_vector_fail.cpp -o v_vector_fail_prog
v_vector_fail.cpp: In function 'int main()':
v_vector_fail.cpp:5:10: error: 'generate' is not a member of 'std'; did you mean 'negate'?
    5 |     std::generate(v.begin(), v.end(), [&n] { return ++n; });
      |          ^~~~~~~~
      |          negate

A program that uses std::generate includes <algorithm>, whether or not it happens to compile without it. A sole <algorithm> file makes the point in miniature - the call compiles and runs with nothing else in sight:

Terminal
-std=c++17 i_algorithm.cpp -o i_algorithm_prog
$ ./i_algorithm_prog

Neighboring headers: 65 fail plainly, 36 fail with the wrong suggestion, and 4 rescue it by accident

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 fills 0, 0, 0, 0, 0 with a counter generator and checks that the array holds 1, 2, 3, 4, 5, the outcome splits by compile result two ways - the failure, or a rescue - and the failure itself comes in exactly two diagnostic shapes. Every diagnostic component was classified separately - the error line, any suggestion tail on it, any note lines, and any help lines. These 65 headers all fail with the plain is not a member of 'std' error line - no suggestion tail on that line, no note lines, and no help lines:

  • <any>
  • <array>
  • <atomic>
  • <barrier>
  • <bit>
  • <cassert>
  • <cctype>
  • <cerrno>
  • <cfenv>
  • <cfloat>
  • <charconv>
  • <chrono>
  • <cinttypes>
  • <ciso646>
  • <climits>
  • <clocale>
  • <cmath>
  • <compare>
  • <concepts>
  • <csetjmp>
  • <csignal>
  • <cstdalign>
  • <cstdarg>
  • <cstdbool>
  • <cstddef>
  • <cstdint>
  • <cstdio>
  • <cstdlib>
  • <cstring>
  • <ctime>
  • <cuchar>
  • <cwchar>
  • <cwctype>
  • <exception>
  • <expected>
  • <format>
  • <initializer_list>
  • <iosfwd>
  • <latch>
  • <limits>
  • <list>
  • <mutex>
  • <new>
  • <numbers>
  • <optional>
  • <ranges>
  • <ratio>
  • <scoped_allocator>
  • <semaphore>
  • <shared_mutex>
  • <source_location>
  • <span>
  • <spanstream>
  • <stacktrace>
  • <stdfloat>
  • <stop_token>
  • <string_view>
  • <syncstream>
  • <tuple>
  • <type_traits>
  • <typeindex>
  • <typeinfo>
  • <utility>
  • <variant>
  • <version>

And these 36 headers fail with the same error line carrying the suggestion tail did you mean 'negate'? - again with no note lines and no help lines:

  • <bitset>
  • <ccomplex>
  • <codecvt>
  • <complex>
  • <condition_variable>
  • <coroutine>
  • <ctgmath>
  • <deque>
  • <filesystem>
  • <forward_list>
  • <fstream>
  • <future>
  • <iomanip>
  • <ios>
  • <iostream>
  • <istream>
  • <iterator>
  • <locale>
  • <map>
  • <memory>
  • <memory_resource>
  • <numeric>
  • <ostream>
  • <queue>
  • <random>
  • <set>
  • <sstream>
  • <stack>
  • <stdexcept>
  • <streambuf>
  • <string>
  • <system_error>
  • <thread>
  • <unordered_map>
  • <unordered_set>
  • <vector>

A file with no include at all fails the plain way. Three entries deserve a pause. <limits> fails plainly: the header of the numeric limits declares nothing close enough to this name to trigger even the wrong guess. <numeric> fails with the suggestion: the header of the accumulating names does not declare this one either - and readers who reach for it because std::iota lives there get the wrong suggestion for their trouble. And <ranges> fails plainly for the classic name: the C++20 ranges library has its own generate name, and it does not bring this one in - that name gets its own section below. The split itself has a simple shape: the suggestion appears exactly in the contexts where some other declaration - std::negate, pulled in on the side - is visible for the compiler to guess from, and it is absent where nothing similar is declared. Neither shape names <algorithm>.

And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:

  • <algorithm>
  • <execution>
  • <functional>
  • <regex>
  • <valarray>

The shape is the family shape: <algorithm> sits on top of the container and stream headers, almost nothing pulls it in on the side, and the four headers that do are headers few fill programs include. That is also why the failing set covers nearly the whole library: with 101 of the other 105 headers failing, the missing include is the normal state of a file that was never given <algorithm>, not an edge case. <algorithm> - the home header - is on the rescue list by definition, not by transitivity, and the other 4 rescues are this library version's behavior, not a guarantee.

The fix

Add the header:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v(5);
    int n = 0;
    std::generate(v.begin(), v.end(), [&n] { return ++n; });
    for (int x : v) std::cout << x << " ";
    std::cout << "\n";
    return 0;
}
Terminal
-std=c++17 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
1 2 3 4 5 

One include, <algorithm>, declares the function - and the textbook program, which failed outright before, now prints 1 2 3 4 5: the counter generator is called once per element, in order, and its successive results 1, 2, 3, 4, 5 land in the vector in that order. What the include changes is the standing of the program as well as its fate: the name is now declared by a header the file names itself, instead of by an internal include of one of the four rescuing headers that a different library version is free to rearrange.

What the call does: one generator call per element, in order, and no return value

The classic call returns nothing - there is no iterator and no count to read back - and what it leaves behind is the filled range itself. Over a vector the counter fills 1, 2, 3, 4, 5; over a list a generator that starts at 10 and adds 10 before returning fills 20, 30, 40, 50, 60; and the counted companion std::generate_n fills only the count it is given, leaving the rest of the range untouched and returning the iterator just past the filled part:

C++
#include <algorithm>
#include <iostream>
#include <list>
#include <vector>
int main() {
    std::vector<int> v(5);
    int n = 0;
    std::generate(v.begin(), v.end(), [&n] { return ++n; });
    std::cout << "vector";
    for (int x : v) std::cout << " " << x;
    std::cout << "\n";
    std::list<int> l(5);
    int m = 10;
    std::generate(l.begin(), l.end(), [&m] { return m += 10; });
    std::cout << "list";
    for (int x : l) std::cout << " " << x;
    std::cout << "\n";
    std::vector<int> p = {9, 9, 9, 9, 9};
    int k = 0;
    auto end = std::generate_n(p.begin(), 3, [&k] { return ++k; });
    std::cout << "generate_n";
    for (int x : p) std::cout << " " << x;
    std::cout << " next " << (end - p.begin()) << "\n";
    return 0;
}
Terminal
-std=c++17 r_semantics.cpp -o r_semantics_prog
$ ./r_semantics_prog
vector 1 2 3 4 5
list 20 30 40 50 60
generate_n 1 2 3 9 9 next 3

Read in order: the vector line reports 1 2 3 4 5 - five calls, five assignments, in range order. The list line reports 20 30 40 50 60 - the same one-call-per-element discipline over a range whose iterators can only be stepped one at a time. The generate_n line reports 1 2 3 9 9 with next 3: of the five 9s only the first three were overwritten, and the returned iterator sits at index 3, just past the last element the counted form touched. That return is the counted form's alone - a caller who needs to know where a classic std::generate stopped has the whole range as the answer, because the classic form always runs to the last iterator.

Generators: a function, a function object, or a lambda

The generator is any callable that takes no arguments and returns a value assignable to the element type, and its state is what makes the fill a sequence instead of a repetition. A plain function keeps its state in a variable outside itself; a function object carries the state inside; a lambda captures it:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int next_square_state = 0;
int next_square() { ++next_square_state; return next_square_state * next_square_state; }
struct Doubler {
    int cur = 1;
    int operator()() { int r = cur; cur *= 2; return r; }
};
int main() {
    std::vector<int> a(4);
    std::generate(a.begin(), a.end(), next_square);
    std::cout << "function";
    for (int x : a) std::cout << " " << x;
    std::cout << "\n";
    std::vector<int> b(4);
    std::generate(b.begin(), b.end(), Doubler{});
    std::cout << "functor";
    for (int x : b) std::cout << " " << x;
    std::cout << "\n";
    std::vector<int> c(4);
    int n = 100;
    std::generate(c.begin(), c.end(), [&n] { return n -= 10; });
    std::cout << "lambda";
    for (int x : c) std::cout << " " << x;
    std::cout << "\n";
    return 0;
}
Terminal
-std=c++17 r_generator_forms.cpp -o r_generator_forms_prog
$ ./r_generator_forms_prog
function 1 4 9 16
functor 1 2 4 8
lambda 90 80 70 60

The function line reports 1 4 9 16: the function's counter advances between calls, so the four elements get the first four squares. The functor line reports 1 2 4 8: the function object's member doubles after each return. The lambda line reports 90 80 70 60: the captured counter is decremented before each return, counting down from 100 by tens. In every case the call invokes the generator exactly once per element and assigns that one result before moving on - a generator with no state, returning the same value every time, fills the range with that value repeated, which is the fill operation wearing a generator's clothes.

The edge ranges: empty and one element

No short range is an error, and the call count tracks the length exactly. An empty range calls the generator zero times - there is no element to assign to - and a one-element range calls it once and stores that single result:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    int calls = 0;
    auto gen = [&calls] { ++calls; return 7; };
    std::vector<int> e;
    std::generate(e.begin(), e.end(), gen);
    std::cout << "empty calls " << calls << "\n";
    std::vector<int> one(1);
    std::generate(one.begin(), one.end(), gen);
    std::cout << "one calls " << calls << " value " << one[0] << "\n";
    std::vector<int> five(5);
    std::generate(five.begin(), five.end(), gen);
    std::cout << "five calls " << calls << "\n";
    return 0;
}
Terminal
-std=c++17 k_empty.cpp -o k_empty_prog
$ ./k_empty_prog
empty calls 0
one calls 1 value 7
five calls 6

The empty line reports calls 0: the generator is never invoked, so a generator whose first call has a side effect the program depends on does nothing at all over an empty range. The one line reports calls 1 and value 7: the single element holds the generator's first result. The five line brings the shared counter to calls 6 - the one call from the one-element fill plus five more, one per element. Only ranges with elements call the generator at all, and every element costs exactly one call.

Forward iterators: the weakest iterators it accepts

The iterator requirement is the forward-iterator one: the range does not need to be indexable, and it does not need to be walkable backward. This program fills a forward list - a container whose iterators step in one direction only:

C++
#include <algorithm>
#include <forward_list>
#include <iostream>
int main() {
    std::forward_list<int> fl(5);
    int n = 0;
    std::generate(fl.begin(), fl.end(), [&n] { return ++n; });
    for (int x : fl) std::cout << x << " ";
    std::cout << "\n";
    return 0;
}
Terminal
-std=c++17 f_forward.cpp -o f_forward_prog
$ ./f_forward_prog
1 2 3 4 5 

It reports 1 2 3 4 5. The fill is a single forward pass - each element is assigned the next generator result, in order - and a forward-only range is enough for it, which is the floor the list line in the semantics section also stands on.

Versions: no second cause - this name predates C++11

This call needs no modern-enough standard, and that is what separates it from some of its neighbors. std::generate predates C++11 - the cppreference page carries no "since" marker on the classic overload. The fixed programs in this guide are compiled as C++17, but there is no floor below that to hit: with <algorithm> included, the same fill compiled as C++98 - where the generator must be a function object, there being no lambdas yet - compiles and runs, and compiled as C++11 with a lambda it does the same:

C++
#include <algorithm>
#include <iostream>
struct Counter {
    int n;
    Counter() : n(0) {}
    int operator()() { return ++n; }
};
int main() {
    int a[5] = {0, 0, 0, 0, 0};
    std::generate(a, a + 5, Counter());
    for (int i = 0; i < 5; ++i) std::cout << a[i] << " ";
    std::cout << "\n";
    return 0;
}
Terminal
-std=c++98 c_version98.cpp -o c_version98_prog
$ ./c_version98_prog
1 2 3 4 5 
Terminal
-std=c++11 c_version11.cpp -o c_version11_prog
$ ./c_version11_prog
1 2 3 4 5 

Both builds print 1 2 3 4 5. So this name's error has one cause, not two: add <algorithm>, and no standard flag needs to change with it. A file that includes <algorithm> and still sees this error does not have a version problem with the classic call; the include is not reaching the file that names the call, or the name in the file is not this name.

The pieces layered on top of the classic call do carry floors, and naming one splits in two when a reader tries it. This name does have an execution-policy overload, since C++17 - and naming the policy with only <algorithm> included fails on the policy's name, not the algorithm's:

C++
#include <algorithm>
#include <vector>
int main() {
    std::vector<int> v(5);
    int n = 0;
    std::generate(std::execution::seq, v.begin(), v.end(), [&n] { return ++n; });
    return v[0] == 1 && v[4] == 5 ? 0 : 1;
}
Output
-std=c++17 p_policy_algonly.cpp -o p_policy_algonly_prog
p_policy_algonly.cpp: In function 'int main()':
p_policy_algonly.cpp:6:24: error: 'std::execution' has not been declared
    6 |     std::generate(std::execution::seq, v.begin(), v.end(), [&n] { return ++n; });
      |                        ^~~~~~~~~

That wording - has not been declared, naming std::execution - is the missing-header shape, and here adding the header is the whole fix: add <execution> alongside <algorithm>, and the same call compiles and runs, because the policy overload of std::generate exists:

Terminal
-std=c++17 p_policy_both.cpp -o p_policy_both_prog
$ ./p_policy_both_prog

The policy form belongs to this name, and it carries the C++17 floor. Compiled as C++14, the same two-header program fails on the policy's name again - at that standard the policy names are not there to be declared:

Output
-std=c++14 p_policy_cpp14.cpp -o p_policy_cpp14_prog
p_policy_cpp14.cpp: In function 'int main()':
p_policy_cpp14.cpp:7:24: error: 'std::execution' has not been declared
    7 |     std::generate(std::execution::seq, v.begin(), v.end(), [&n] { return ++n; });
      |                        ^~~~~~~~~

And since C++20 the classic call is also usable in constant expressions - this program's fill happens at compile time, and the assertion on the filled array is part of the build:

C++
#include <algorithm>
#include <array>
constexpr bool check() {
    std::array<int, 5> a{};
    int n = 0;
    std::generate(a.begin(), a.end(), [&n] { return ++n; });
    return a[0] == 1 && a[4] == 5;
}
static_assert(check());
int main() { return 0; }
Terminal
-std=c++20 c_constexpr20.cpp -o c_constexpr20_prog
$ ./c_constexpr20_prog

Compiled as C++17, the same program fails - the call is not a constant expression there, and the static assertion cannot hold:

Output
-std=c++17 c_constexpr17.cpp -o c_constexpr17_prog
c_constexpr17.cpp: In function 'constexpr bool check()':
c_constexpr17.cpp:6:18: warning: call to non-'constexpr' function 'void std::generate(_FIter, _FIter, _Generator) [with _FIter = int*; _Generator = check()::<lambda()>]' [-Winvalid-constexpr]
    6 |     std::generate(a.begin(), a.end(), [&n] { return ++n; });
      |     ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/algorithm:61,
                 from c_constexpr17.cpp:1:
/usr/include/c++/13/bits/stl_algo.h:4434:5: note: 'void std::generate(_FIter, _FIter, _Generator) [with _FIter = int*; _Generator = check()::<lambda()>]' declared here
 4434 |     generate(_ForwardIterator __first, _ForwardIterator __last,
      |     ^~~~~~~~
c_constexpr17.cpp: At global scope:
c_constexpr17.cpp:9:20: error: non-constant condition for static assertion
    9 | static_assert(check());
      |               ~~~~~^~
c_constexpr17.cpp:9:20: error: 'constexpr bool check()' called in a constant expression
c_constexpr17.cpp:3:16: note: 'constexpr bool check()' declared here
    3 | constexpr bool check() {
      |                ^~~~~

So C++20 is the floor for that layered piece and C++17 is the floor for the policy form - and no standard at any level rescues a missing include, while no include is needed beyond <algorithm> for the classic call at any standard level.

The neighboring names: generate_n shares the failure, fill escapes it, and iota lives elsewhere

The three names a reader most often reaches for alongside this one split three ways. std::generate_n is this name's counted companion, declared by <algorithm> alongside it, and it shares the failure minus the decoration: in a file whose only include is <vector>, a std::generate_n call fails with the plain error - no negate suggestion, even though the same file gives std::generate the suggestion:

Output
-std=c++17 b_generate_n_vector.cpp -o b_generate_n_vector_prog
b_generate_n_vector.cpp: In function 'int main()':
b_generate_n_vector.cpp:5:10: error: 'generate_n' is not a member of 'std'
    5 |     std::generate_n(v.begin(), 5, [&n] { return ++n; });
      |          ^~~~~~~~~~

Its own sweep, run the same way as the main one, lands on the same 5 rescues - <algorithm> and the same 4 transitive headers - with all 101 other headers failing plainly: the counted form never earns the wrong suggestion in any probed context.

std::fill is declared by <algorithm> by rule too - it copy-assigns one given value to every element - but it does not share this failure in practice: in a file whose only include is <vector>, a std::fill call compiles and runs in this library:

Terminal
-std=c++17 b_fill_vector.cpp -o b_fill_vector_prog
$ ./b_fill_vector_prog

That compile is a transitive rescue, not a rule: some header inside <vector>'s own includes declares fill on the side in this library version. A complete program still names <algorithm> for it - the rescue is exactly the kind that disappears when a library rearranges its internal includes.

And std::iota - which fills a range with successive increments of a starting value, the sequence this guide's counter generator imitates - does not live in <algorithm> at all. It is declared by <numeric>, and it has existed only since C++11. In a file whose only include is <vector> it fails plainly:

Output
-std=c++17 b_iota_vector.cpp -o b_iota_vector_prog
b_iota_vector.cpp: In function 'int main()':
b_iota_vector.cpp:4:10: error: 'iota' is not a member of 'std'
    4 |     std::iota(v.begin(), v.end(), 1);
      |          ^~~~
  • and, the trap, in a file that includes <algorithm> alongside <vector> it still fails plainly, because the added header is the wrong home for this name:
Output
-std=c++17 b_iota_algorithm_only.cpp -o b_iota_algorithm_only_prog
b_iota_algorithm_only.cpp: In function 'int main()':
b_iota_algorithm_only.cpp:5:10: error: 'iota' is not a member of 'std'
    5 |     std::iota(v.begin(), v.end(), 1);
      |          ^~~~

Three neighbors, three different relationships to the one added header: generate_n is fixed by it, fill is declared by it but often compiles without it, and iota is not declared by it at all. They are shown here only as neighbors, not re-taught.

The neighboring names, fixed

Together the four names take two headers between them - <algorithm> for generate, generate_n, and fill, and <numeric> for iota, with <vector> covering only the container they fill. The fixed neighbor program reports all four fills over five-element ranges:

C++
#include <algorithm>
#include <iostream>
#include <numeric>
#include <vector>
int main() {
    std::vector<int> v(5);
    int n = 0;
    std::generate(v.begin(), v.end(), [&n] { return ++n; });
    std::cout << "generate";
    for (int x : v) std::cout << " " << x;
    std::cout << "\n";
    std::vector<int> w(5, 9);
    int k = 0;
    std::generate_n(w.begin(), 3, [&k] { return ++k; });
    std::cout << "generate_n";
    for (int x : w) std::cout << " " << x;
    std::cout << "\n";
    std::fill(v.begin(), v.end(), 7);
    std::cout << "fill";
    for (int x : v) std::cout << " " << x;
    std::cout << "\n";
    std::iota(v.begin(), v.end(), 1);
    std::cout << "iota";
    for (int x : v) std::cout << " " << x;
    std::cout << "\n";
    return 0;
}
Terminal
-std=c++17 b_neighbors_fixed.cpp -o b_neighbors_fixed_prog
$ ./b_neighbors_fixed_prog
generate 1 2 3 4 5
generate_n 1 2 3 9 9
fill 7 7 7 7 7
iota 1 2 3 4 5

Read in order: generate fills 1, 2, 3, 4, 5 from the counter; generate_n fills only the first three of the 9s, leaving 1 2 3 9 9; fill writes the one value everywhere, 7 7 7 7 7; and iota writes the incrementing sequence 1 2 3 4 5 from its starting value - four different ways to put values into a range, answered from two headers, neither of them the container's own.

A ranges form since C++20, in the same header

C++20 added a ranges form of this name, and the standard declares it in the same header as the classic name. std::ranges::generate is a function object in the ranges namespace, declared in <algorithm>, that also accepts a whole range - and unlike the classic call it returns a value: the past-the-end iterator of the filled range. Compiled as C++20 it fills the same five-element vector with 1, 2, 3, 4, 5 and reports where the fill ended:

C++
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v(5);
    int n = 0;
    auto end = std::ranges::generate(v, [&n] { return ++n; });
    std::cout << "ranges";
    for (int x : v) std::cout << " " << x;
    std::cout << " end-at " << (end - v.begin()) << "\n";
    return 0;
}
Terminal
-std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
ranges 1 2 3 4 5 end-at 5

Two boundaries and one library behavior keep the forms straight. The ranges form needs C++20: compiled as C++17, that same program fails on the namespace itself, with 'std::ranges' has not been declared:

Output
-std=c++17 r_ranges_cpp17.cpp -o r_ranges_cpp17_prog
r_ranges_cpp17.cpp: In function 'int main()':
r_ranges_cpp17.cpp:7:21: error: 'std::ranges' has not been declared
    7 |     auto end = std::ranges::generate(v, [&n] { return ++n; });
      |                     ^~~~~~

The ranges header is not the ranges form's home in this library: a file whose only include is <ranges> fails for it, with an error that names the namespace the reader used:

Output
-std=c++20 r_ranges_rangesheader.cpp -o r_ranges_rangesheader_prog
r_ranges_rangesheader.cpp: In function 'int main()':
r_ranges_rangesheader.cpp:5:18: error: 'generate' is not a member of 'std::ranges'
    5 |     std::ranges::generate(a, a + 5, [&n] { return ++n; });
      |                  ^~~~~~~~

A file that includes <array> alone fails the same way:

Output
-std=c++20 r_ranges_arrayonly.cpp -o r_ranges_arrayonly_prog
r_ranges_arrayonly.cpp: In function 'int main()':
r_ranges_arrayonly.cpp:5:18: error: 'generate' is not a member of 'std::ranges'
    5 |     std::ranges::generate(a, [&n] { return ++n; });
      |                  ^~~~~~~~

That is the opposite of the neighboring adjacent-pair name's behavior in this family, whose ranges form a sole <ranges> file does compile - here it does not, and the header the standard names for both forms is <algorithm>, so a complete program names it. The classic name, meanwhile, is not pulled in by the ranges header either: in the sweep probed here, a file whose only include is <ranges> fails for std::generate with the plain error, as the sweep section above lists.

A different wording - and an argument-dependent rescue that depends on the iterators' types

Including <algorithm> is not the only half of the name. Over plain arrays, the unqualified call fails:

C++
#include <algorithm>
int main() {
    int a[5] = {0, 0, 0, 0, 0};
    int n = 0;
    generate(a, a + 5, [&n] { return ++n; });
    return a[0] == 1 && a[4] == 5 ? 0 : 1;
}
Output
-std=c++17 g_unqualified.cpp -o g_unqualified_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:5:5: error: 'generate' was not declared in this scope; did you mean 'std::generate'?
    5 |     generate(a, a + 5, [&n] { return ++n; });
      |     ^~~~~~~~
      |     std::generate
In file included from /usr/include/c++/13/algorithm:73,
                 from g_unqualified.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:200:1: note: 'std::generate' declared here
  200 | generate(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Generator __g);
      | ^~~~~~~~

That wording - was not declared in this scope, with did you mean 'std::generate'? 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::generate; adding includes does not bring the name out of namespace std. Swap the arrays for a vector, and the same unqualified call compiles and runs:

C++
#include <algorithm>
#include <vector>
int main() {
    std::vector<int> v(5);
    int n = 0;
    generate(v.begin(), v.end(), [&n] { return ++n; });
    return v[0] == 1 && v[4] == 5 ? 0 : 1;
}
Terminal
-std=c++17 g_unqualified_vec.cpp -o g_unqualified_vec_prog
$ ./g_unqualified_vec_prog

That compile is argument-dependent lookup at work, not a rule to rely on: the vector's iterators are std types, so their associated namespace is std, and lookup finds std::generate through them. A list's iterators rescue it the same way - the same unqualified call over a list compiles and runs:

Terminal
-std=c++17 g_unqualified_list.cpp -o g_unqualified_list_prog
$ ./g_unqualified_list_prog

But the container being a std type is not enough by itself - over a std::array, the same unqualified call fails with the same wording, because in this library a std::array's iterators are plain pointers, and a pointer has no associated namespace:

C++
#include <algorithm>
#include <array>
int main() {
    std::array<int, 5> v{};
    int n = 0;
    generate(v.begin(), v.end(), [&n] { return ++n; });
    return v[0] == 1 && v[4] == 5 ? 0 : 1;
}
Output
-std=c++17 g_unqualified_stdarray.cpp -o g_unqualified_stdarray_prog
g_unqualified_stdarray.cpp: In function 'int main()':
g_unqualified_stdarray.cpp:6:5: error: 'generate' was not declared in this scope; did you mean 'std::generate'?
    6 |     generate(v.begin(), v.end(), [&n] { return ++n; });
      |     ^~~~~~~~
      |     std::generate
In file included from /usr/include/c++/13/algorithm:73,
                 from g_unqualified_stdarray.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:200:1: note: 'std::generate' declared here
  200 | generate(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, _Generator __g);
      | ^~~~~~~~

The pattern across the probed set is exact: the rescue follows the iterator type, not the container's name - class iterators of std containers carry the name in, pointer iterators do not, whatever container stands behind them. Write the qualifier either way.

The exact rule

  • std::generate is declared by #include <algorithm>, and by no other header by rule. In the sweep probed here, 101 of the other 105 headers in the library's header set - among them <vector>, <iostream>, <iterator>, <numeric>, <ranges>, and <limits> - do not declare it, and neither does a file with no include at all; the remaining 4 pull it in transitively, as this library version's behavior, not as a guarantee.
  • 'generate' is not a member of 'std' has one cause in the contexts probed here: the <algorithm> include is missing and nothing else in the file pulled the name in - and it comes in two shapes in the sweep probed here: 65 failing sole headers fail with one error line and no suggestion tail, no note lines, and no help lines, while 36 fail with the suggestion tail did you mean 'negate'? attached. The suggestion is wrong in every case: std::negate negates one argument and cannot fill a range, and following it does not fix the program.
  • In the sweep probed here, 4 headers besides <algorithm> rescue std::generate 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::generate in.
  • The result rules are the documented ones, as the fixed programs here show: the fixed program prints 1 2 3 4 5 for the five-element vector; the semantics program prints vector 1 2 3 4 5, list 20 30 40 50 60, and generate_n 1 2 3 9 9 next 3 - the classic call assigns one generator result per element in order and returns nothing, while the counted form fills only its count and returns the iterator just past the filled part.
  • The edge ranges follow from the per-element rule: an empty range calls the generator 0 times and a one-element range calls it once - the edge program here prints empty calls 0, one calls 1 value 7, and five calls 6 for the running total across its three fills.
  • The classic std::generate predates C++11: with <algorithm> included it compiles and runs as C++98 with a function-object generator and as C++11 with a lambda, reporting 1 2 3 4 5 in both builds here. The layered pieces have floors: the execution-policy overload starts at C++17 - with only <algorithm> included, naming std::execution::seq fails with 'std::execution' has not been declared; with <execution> included alongside it, the policy call compiles and runs; and compiled as C++14, the same program fails on the policy's name. Constant-expression use starts at C++20: the static-assert program here compiles as C++20 and fails as C++17.
  • The iterators must meet the forward-iterator requirements. The forward-list program here shows the floor in action: over a forward list, whose iterators step in one direction only, the call fills 1 2 3 4 5.
  • The neighbors split three ways: std::generate_n is declared by #include <algorithm> as well - in the programs here, a generate_n call in a file whose only include is <vector> fails with the plain error, its own sweep lands on the same 5 rescues with all 101 other headers failing plainly, and the fixed neighbor program prints its counted fill. std::fill is declared by <algorithm> by rule but compiles in a sole <vector> file in this library - a transitive rescue, not a guarantee. std::iota is declared by #include <numeric>, not <algorithm>: it fails plainly in a sole <vector> file and still fails plainly with <algorithm> included, and the fixed neighbor program, which includes <algorithm>, <numeric>, and <vector>, prints generate 1 2 3 4 5, generate_n 1 2 3 9 9, fill 7 7 7 7 7, and iota 1 2 3 4 5.
  • std::ranges::generate is a ranges form of this same name: a function object in namespace std::ranges, declared in <algorithm> by the standard, needing C++20, returning the past-the-end iterator of the filled range - compiled as C++17 the ranges program here fails with 'std::ranges' has not been declared, and compiled as C++20 it prints ranges 1 2 3 4 5 end-at 5 for the five-element vector. In the probes here <ranges> alone fails for it with 'generate' is not a member of 'std::ranges', <array> alone fails the same way, and <ranges> fails plainly for the classic name in the sweep.
  • 'generate' was not declared in this scope for an unqualified generate use is a different defect: qualify the name as std::generate - the include alone does not bring the name out of namespace std. For this name the unqualified call's fate depends on the iterators' types: over plain arrays it fails, as the first unqualified program above shows; over a vector's iterators and over a list's iterators it is rescued through argument-dependent lookup, as the second and third show, because those iterators are std class types; over a std::array's iterators it fails again, because in this library those iterators are plain pointers. Qualify the name either way.

The fixes, ranked

  • First, check the include list of the file that names std::generate. If <algorithm> is not there, add it - that is the fix for the only cause of this error. It holds whether the file failed plainly, failed with the negate suggestion, or happened to compile before through one of the 4 transitive rescues and stopped compiling when its other includes changed.
  • Second, do not follow the negate suggestion: std::negate is a unary negation functor, not a range fill, and no program that needs std::generate is fixed by renaming the call to it. The suggestion is a spelling guess, and for this name it is wrong in all 36 probed contexts where it appears.
  • Third, do not chase a standard flag for the classic call: this name predates C++11, so no flag change fixes this error, and a file with <algorithm> included that still fails has an include that is not reaching the translation unit, not a version problem.
  • Fourth, do not reach for a container's header because the call fills a container, for <iterator> because the call takes iterators, or for <numeric> because the call writes a sequence. In the sweep probed here all three fail for this name - the declaring header is <algorithm>, and a complete program names it alongside the container's own header.
  • Fifth, do not remove <algorithm> because a file compiles without it: in the probed set only 4 headers rescue the classic name, and those rescues are not guaranteed - a file that drops the include is one internal-include change away from the error in the first section. The same warning covers std::fill, which compiles in a sole <vector> file here only transitively.
  • Sixth, if the error names std::execution instead - 'std::execution' has not been declared - add <execution> alongside <algorithm> and keep the standard at C++17 or later: this name does have a policy overload, and that pair of changes is the whole fix for the policy form.
  • Seventh, match the standard to the one piece beyond the classic call that has a floor: constant-expression use needs C++20, and the ranges form std::ranges::generate needs C++20 - with its header still <algorithm> by the standard, not <ranges>.
  • Eighth, if the error says was not declared in this scope instead, fix the name's qualifier to std::generate as well - and do not rely on argument-dependent lookup to rescue the unqualified call, because a plain array and a std::array each remove that rescue in the probed set.
  • Ninth, if the failing name is a neighboring one, check which home it actually has: generate_n and fill take an added <algorithm>, but iota takes <numeric> - the one neighbor for which adding this guide's header changes nothing.

Which fix for which cause

  • #include <algorithm> missing, std::generate used in a file whose only include is one of the 65 headers listed as plain-failing in the sweep section above, or that includes nothing: the message is 'generate' is not a member of 'std', with no suggestion tail, no note lines, and no help lines - add #include <algorithm>.
  • #include <algorithm> missing, std::generate used in a file whose only include is one of the 36 headers listed as suggestion-failing in the sweep section above: the message is 'generate' is not a member of 'std'; did you mean 'negate'? - add #include <algorithm> anyway, and do not rename the call to negate.
  • std::generate used in a file whose only include is one of the 4 transitive-rescue headers listed in the sweep section above - <execution>, <functional>, <regex>, <valarray>: no error on this library version - add #include <algorithm> anyway, because those rescues are not guaranteed.
  • std::generate used in a textbook file including <iostream> and <vector>: the error above, with the negate suggestion - add #include <algorithm>; the textbook includes do not rescue this name, and the suggestion they trigger is the wrong function.
  • std::generate used with <algorithm> included, at any standard level from C++98 up: no version error - the classic call predates C++11, and the same fill reports 1 2 3 4 5 compiled as C++98 and as C++11.
  • std::generate called with an execution policy, with <algorithm> included but <execution> not: the message is 'std::execution' has not been declared - add <execution>; the policy overload exists for this name since C++17, and compiled as C++14 the same program fails on the policy's name for the standard's reason.
  • std::iota used with <algorithm> included but <numeric> not: the message is 'iota' is not a member of 'std', plain - add <numeric>; this neighbor's home is the other header, and it exists only since C++11.
  • generate used without the std:: qualifier, <algorithm> included, over plain-array arguments or a std::array's iterators: the message is 'generate' was not declared in this scope; did you mean 'std::generate'? - qualify the name as std::generate. Over a vector's iterators or a list's iterators, the same unqualified call compiles through argument-dependent lookup - qualify it anyway, so the call does not depend on the iterators' types.

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 import the container's header, the iterator header, or the numeric header into this error as the fix: std::generate and two of its neighbors take <algorithm> and std::vector takes <vector>, so there are two headers in a complete fill program, one for the fill operations and one for the container - and a third, <numeric>, only when std::iota is also named.
  • Do not assume the textbook includes rescue this name by rule, and do not delete a working <algorithm> line because a file compiles without it. In the sweep here <iostream> and <vector> fail for std::generate - with the wrong negate suggestion attached - and the 4 headers that rescue it do so only as this library version's behavior.
  • Read what the call leaves behind, not a return value: the classic call returns nothing, so the filled range is the whole result - the fixed programs here print 1 2 3 4 5 for the textbook case, and vector 1 2 3 4 5, list 20 30 40 50 60, and generate_n 1 2 3 9 9 next 3 for the semantics cases.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 1 2 3 4 5 under C++98 and C++11, 1 2 3 4 5 for the forward list, ranges 1 2 3 4 5 end-at 5 for the ranges form at C++20, and the four neighbor fills for the neighbor calls.
  • 'std::fill' Is Not a Member of 'std' Fix - the constant-value member of this guide's family: std::fill copy-assigns one value to every element, from <algorithm> like this guide's name, where std::generate asks a generator for a fresh value at each element - and in this library fill compiles in a sole <vector> file that generate fails in.
  • 'std::iota' Is Not a Member of 'std' Fix - the sequence member of the family, from the other header: std::iota fills a range with successive increments of a starting value, from <numeric> - the fill this guide's counter generator imitates by hand, and the one neighbor that adding <algorithm> does not fix.

Sources

  • cppreference, std::generate — for std::generate being a function template defined in <algorithm>; for it assigning the result of successive evaluations of the generator to each element of the range; for the classic call returning nothing; for the iterators needing to meet the forward-iterator requirements; for the call making exactly N evaluations of the generator and N assignments over a range of N elements; for the classic overload carrying no "since" marker - the name predates C++11 - and for the execution-policy overload existing since C++17 and constant-expression use since C++20.
  • cppreference, std::generate_n — for std::generate_n being defined in <algorithm> as well; for it filling only the first count elements when the count is positive and doing nothing when the count is non-positive; and for it returning the iterator just past the last element filled.
  • cppreference, std::ranges::generate — for the ranges form existing since C++20, being defined in <algorithm>, and returning the past-the-end iterator of the filled range.
  • cppreference, Standard library header <algorithm> — for <algorithm> being the header whose synopsis declares std::generate, std::generate_n, std::fill, and the C++20 std::ranges::generate, the home-header fact the fix, the neighbor, and the ranges sections of this guide rest on.

Keep Exploring