C++

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

std::fill lives in one header, <algorithm> - but many common headers pull it in on the side, so this error hides in textbook files and surfaces in lean ones: a file whose only include is <type_traits> or <utility> gets it in full. This guide pins down when the error appears, which probed headers do and do not cause it, and the one include that fixes it by specification.

Written November 30, 2026 · Tested with GCC 13.3.0 · 12 min read

By the end of this guide
  • The error: the include is missing
  • The textbook version compiles - the common headers rescue it
  • Neighboring headers: which ones rescue it, which ones do not
  • The fix

Written November 30, 2026

Tested with GCC 13.3.0

What this guide is

std::fill is a function template defined in the header <algorithm>. It assigns the given value to every element in the range [first, last).

Because the template lives in one specific header, 'fill' 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 GCC prints no note naming <algorithm> and, in the contexts probed here, no suggestion at all, so the fix has to come from knowing the name's home, not from reading a note.

What makes this error easy to miss is that many common headers do rescue it: <vector>, <string>, and <iostream> pull the name in on this library version, so an ordinary file that fills a vector and prints the result compiles without <algorithm>. The error surfaces in lean files - a header, a test, or a small translation unit whose includes are few. Each case is separated below.

The error: the include is missing

This program fills the array {1, 2, 3} with 7 using std::fill, but includes only <type_traits>:

C++
#include <type_traits>
int main() {
    int a[] = {1, 2, 3};
    std::fill(a, a + 3, 7);
    return a[0] == 7 && a[2] == 7 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 a_type_traits.cpp -o a_prog
a_type_traits.cpp: In function 'int main()':
a_type_traits.cpp:4:10: error: 'fill' is not a member of 'std'
    4 |     std::fill(a, a + 3, 7);
      |          ^~~~

That is the whole output: one error, no note, no suggestion. The same program with no include at all fails the same way:

C++
int main() {
    int a[] = {1, 2, 3};
    std::fill(a, a + 3, 7);
    return a[0] == 7 && a[2] == 7 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:10: error: 'fill' is not a member of 'std'
    3 |     std::fill(a, a + 3, 7);
      |          ^~~~

The textbook version compiles - the common headers rescue it

The version most often written first - fill a vector and print the elements with std::cout - compiles without <algorithm>:

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 3};
    std::fill(v.begin(), v.end(), 7);
    for (int x : v) std::cout << x << " ";
    std::cout << "\n";
    return 0;
}

That program builds and runs, printing 7 7 7, even though <algorithm> is nowhere in it. What happened: <vector> pulls the name in transitively on this library version, so the call succeeds in a textbook file while the same call fails in a lean one. Either header alone is enough - a file with only <iostream> that fills a plain array also compiles. No header except <algorithm> is required to declare std::fill, and another library version could leave it out of <vector> and <iostream> too. A program that uses std::fill includes <algorithm> rather than relying on the rescue.

Neighboring headers: which ones rescue it, which ones do not

In a sweep where each header below was compiled as the only header in a program that fills the array {1, 2, 3} with 7, the outcome splits two ways. These headers fail with the plain is not a member of 'std' error, with no note and no suggestion:

  • <type_traits>
  • <utility>
  • <numeric>
  • <limits>
  • <mutex>
  • <optional>
  • <cstring>

A file with no include at all fails the same way. These headers rescue the program - each compiles and runs with no <algorithm> anywhere in the file:

  • <algorithm> itself
  • <vector>
  • <string>
  • <iostream>
  • <functional>
  • <array>
  • <memory>

Those rescues are properties of this library version, not rules of the language - no header except <algorithm> is required to declare std::fill, and another library could leave it out of any of them.

This program includes only <utility>:

C++
#include <utility>
int main() {
    int a[] = {1, 2, 3};
    std::fill(a, a + 3, 7);
    return a[0] == 7 && a[2] == 7 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 s_utility.cpp -o s_utility_prog
s_utility.cpp: In function 'int main()':
s_utility.cpp:4:10: error: 'fill' is not a member of 'std'
    4 |     std::fill(a, a + 3, 7);
      |          ^~~~

And this one includes only <numeric> - a header full of algorithms of its own, which still does not declare this one:

C++
#include <numeric>
int main() {
    int a[] = {1, 2, 3};
    std::fill(a, a + 3, 7);
    return a[0] == 7 && a[2] == 7 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 s_numeric.cpp -o s_numeric_prog
s_numeric.cpp: In function 'int main()':
s_numeric.cpp:4:10: error: 'fill' is not a member of 'std'
    4 |     std::fill(a, a + 3, 7);
      |          ^~~~

The fix

Add the header:

C++
#include <algorithm>
#include <iostream>
int main() {
    int a[] = {1, 2, 3};
    std::fill(a, a + 3, 7);
    for (int x : a) std::cout << x << " ";
    std::cout << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 h_fixed.cpp -o h_prog
$ ./h_prog
7 7 7

One include, <algorithm>, declares the template. std::fill(a, a + 3, 7) assigns 7 to every element of the array, so the program prints 7 7 7.

The neighboring names: the whole header family fails too

The other names declared by <algorithm> fail with the same shape of error in the same lean context. std::fill_n - the function template on the same reference family that assigns a value to the first n elements of a range - gets its own error, again with no header note and no suggestion:

C++
#include <type_traits>
int main() {
    int a[] = {1, 2, 3};
    std::fill_n(a, 3, 7);
    return a[0] == 7 && a[2] == 7 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 b_fill_n.cpp -o b_prog
b_fill_n.cpp: In function 'int main()':
b_fill_n.cpp:4:10: error: 'fill_n' is not a member of 'std'
    4 |     std::fill_n(a, 3, 7);
      |          ^~~~~~

std::count, the occurrence-counting function in the same header, fails the same way in this lean context - it has its own guide, so it is shown here only briefly:

C++
#include <type_traits>
int main() {
    int a[] = {1, 2, 2, 3};
    auto n = std::count(a, a + 4, 2);
    return n == 2 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 c_count.cpp -o c_prog
c_count.cpp: In function 'int main()':
c_count.cpp:4:19: error: 'count' is not a member of 'std'
    4 |     auto n = std::count(a, a + 4, 2);
      |                   ^~~~~

And std::find, the search function in the same header, fails the same way:

C++
#include <type_traits>
int main() {
    int a[] = {1, 2, 3};
    auto it = std::find(a, a + 3, 2);
    return it != a + 3 && *it == 2 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 d_find.cpp -o d_prog
d_find.cpp: In function 'int main()':
d_find.cpp:4:20: error: 'find' is not a member of 'std'
    4 |     auto it = std::find(a, a + 3, 2);
      |                    ^~~~

So does std::sort, the sorting function in the same header:

C++
#include <type_traits>
int main() {
    int a[] = {3, 1, 2};
    std::sort(a, a + 3);
    return a[0] == 1 && a[2] == 3 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 e_sort.cpp -o e_prog
e_sort.cpp: In function 'int main()':
e_sort.cpp:4:10: error: 'sort' is not a member of 'std'
    4 |     std::sort(a, a + 3);
      |          ^~~~

One added include declares every one of these names: the fill, the counted fill, the count, the search, and the sort.

The neighboring names, fixed

With <algorithm> added, std::fill works on more than a plain array. The vector {1, 2, 3} prints as 7 7 7 after the fill, and the string "veldica" becomes xxxxxxx - seven x characters, one for each letter it held:

C++
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 3};
    std::fill(v.begin(), v.end(), 7);
    for (int x : v) std::cout << x << " ";
    std::cout << "\n";
    std::string s = "veldica";
    std::fill(s.begin(), s.end(), 'x');
    std::cout << s << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 i_vector_string.cpp -o i_prog
$ ./i_prog
7 7 7
xxxxxxx

And std::fill_n with a count of 3 fills only the first three elements of {1, 2, 3, 4}, so the program prints 9 9 9 4 - the last element keeps its old value:

C++
#include <algorithm>
#include <iostream>
int main() {
    int a[] = {1, 2, 3, 4};
    std::fill_n(a, 3, 9);
    for (int x : a) std::cout << x << " ";
    std::cout << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 j_fill_n.cpp -o j_prog
$ ./j_prog
9 9 9 4

Since C++20 the same header also declares std::ranges::fill, which takes the whole range instead of an iterator pair. With <algorithm> included it fills the vector {1, 2, 3} and the program prints 7 7 7; in the lean context - only <type_traits> and <vector> included - it fails with its own wording, 'fill' is not a member of 'std::ranges', and there GCC does add a suggestion, did you mean 'std::fill'?, with a note pointing at the std::fill that <vector> pulled in - the ranges name itself still needs <algorithm>.

A different wording: the name without its qualifier

Including <algorithm> is not the only half of the name. This program includes the header but drops the std:: qualifier:

C++
#include <algorithm>
#include <iostream>
int main() {
    int a[] = {1, 2, 3};
    fill(a, a + 3, 7);
    for (int x : a) std::cout << x << " ";
    std::cout << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 g_unqualified.cpp -o g_prog
g_unqualified.cpp: In function 'int main()':
g_unqualified.cpp:5:5: error: 'fill' was not declared in this scope
    5 |     fill(a, a + 3, 7);
      |     ^~~~
g_unqualified.cpp:5:5: note: suggested alternatives:
In file included from /usr/include/c++/13/algorithm:73,
                 from g_unqualified.cpp:1:
/usr/include/c++/13/pstl/glue_algorithm_defs.h:191:1: note:   'std::fill'
  191 | fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value);
      | ^~~~
In file included from /usr/include/c++/13/bits/ranges_algo.h:38,
                 from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algobase.h:595:30: note:   'std::ranges::fill'
  595 |   inline constexpr __fill_fn fill{};
      |                              ^~~~

That wording - was not declared in this scope - is a different defect with a different fix: the header is present, the qualifier is not. Qualify the name as std::fill; adding includes does not bring the name out of namespace std. Note that the std::fill the suggestion points at is the execution-policy overload declared at that line, not the three-argument overload the call needs - the suggestion names the namespace, and the qualifier is the fix either way.

The exact rule

  • std::fill is declared by #include <algorithm>, and nowhere else by rule. In the sweep probed here, <type_traits>, <utility>, <numeric>, <limits>, <mutex>, <optional>, and <cstring> do not declare it, and neither does a file with no include at all.
  • 'fill' is not a member of 'std' means the <algorithm> include is missing and nothing else in the file pulled the name in - and, in the contexts probed here, GCC prints no note naming <algorithm> and no suggestion under it.
  • In the sweep probed here, <vector>, <string>, <iostream>, <functional>, <array>, and <memory> rescue std::fill transitively: a file whose only include is any one of them compiles and runs the call without <algorithm>. That is this library version's behavior, not a guarantee - no header is required to pull std::fill in.
  • std::fill_n, std::count, std::find, and std::sort are declared by the same #include <algorithm>: in the programs shown here, which include only <type_traits> beside the use, each gets its own is not a member of 'std' error when that include is missing, with no header note and no suggestion.
  • std::ranges::fill is declared by the same #include <algorithm> since C++20: with the include it fills a whole range passed as one argument, and in the lean context probed here it gets the error 'fill' is not a member of 'std::ranges' when that include is missing.
  • 'fill' was not declared in this scope for an unqualified fill use is a different defect: qualify the name as std::fill - the include alone does not bring the name out of namespace std.

The fixes, ranked

  • First, check the include list of the file that names std::fill. If <algorithm> is not there, add it - that is the fix in the common case, and it holds whether or not the file happened to compile before through a transitive rescue.
  • Second, do not be reassured by the file compiling in a larger program. In the sweep here <vector>, <string>, and <iostream> rescue the name, so the missing <algorithm> stays hidden until the code moves to a leaner file - add the include where the name is used.
  • Third, do not wait for a note naming the header. For this error, in the contexts probed here, GCC prints none - take the fix from the name's home header, <algorithm>.
  • Fourth, if the error says was not declared in this scope instead, fix the name's qualifier to std::fill as well.
  • Fifth, if the failing name is a neighboring one - fill_n, count, find, or sort - the same added include fixes it: every one of those names shares the header, and none of them gets a header note in the programs here either.

Which fix for which cause

  • #include <algorithm> missing, std::fill used in a file whose only include is <type_traits>, <utility>, <numeric>, <limits>, <mutex>, <optional>, or <cstring>, or that includes nothing: the message is 'fill' is not a member of 'std', with no header note and no suggestion - add #include <algorithm>.
  • std::fill used with only <vector>, <string>, <iostream>, <functional>, <array>, or <memory> included: no error on this library version - add #include <algorithm> anyway, because the rescue is not guaranteed.
  • std::fill_n, std::count, std::find, or std::sort used without <algorithm> in the lean context: the message names that name, with no header note - add #include <algorithm>.
  • fill used without the std:: qualifier, <algorithm> included: the message is 'fill' was not declared in this scope - qualify the name as std::fill.

Checklist

  • Check the include list of the file that names the fill, not just the project. The include must be in that file, or in a header that file includes directly by design.
  • Do not wait for a note naming the header. For this error, in the contexts probed here, GCC prints none - take the fix from the name's home header, <algorithm>, instead.
  • Do not be reassured by <iostream> or <vector> being present. In the sweep here both rescue std::fill, so a compiling file can still be missing <algorithm> - the defect shows up when the code is copied into a leaner file.
  • One header per name the file uses: std::vector comes from <vector> and std::string from <string>, even in a program whose subject is the fill - and std::fill comes from <algorithm> in return.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 7 7 7 for the filled array, 7 7 7 and xxxxxxx for the vector and the string, 9 9 9 4 for the counted fill, and 7 7 7 for the ranges fill.
  • 'std::count' Is Not a Member of 'std' Fix - the same missing-include shape for the counting function in <algorithm>; there, almost no header rescues the name and the textbook form fails, here many headers rescue it and the textbook form compiles.
  • 'std::find' Is Not a Member of 'std' Fix - the same missing-include shape for the search function in <algorithm>, a neighboring name in the same header that fails alongside this guide's name in the lean context.
  • 'std::sort' Is Not a Member of 'std' Fix - the same missing-include shape for the sorting function in <algorithm>, a neighboring name in the same header that fails alongside this guide's name in the lean context.
  • 'std::reverse' Is Not a Member of 'std' Fix - the same missing-include shape for the reversal function in <algorithm>; there, as with std::count, the ordinary container and printing headers do not rescue the name.

Sources

  • cppreference, std::fill — for std::fill being a function template defined in <algorithm> that assigns the given value to all elements in the range [first, last).
  • cppreference, std::fill_n — for std::fill_n being a function template defined in <algorithm> that assigns the given value to the first n elements of a range.
  • cppreference, Standard library header <algorithm> — for <algorithm> being the header that declares the std::fill, std::fill_n, std::count, std::find, and std::sort function templates.
  • cppreference, std::ranges::fill, std::ranges::fill_n — for std::ranges::fill being an algorithm function object defined in <algorithm> since C++20 that assigns a value to all elements in a range, with an overload that takes the whole range as one argument.

Keep Exploring