C++

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

std::count lives in one header, <algorithm> - and almost nothing pulls it in on the side, so this error shows up even in ordinary textbook files: counting occurrences in a vector and printing the result fails the same way. This guide pins down when the error appears, which probed headers do and do not cause it, why the suggestions GCC sometimes adds point at the wrong names, and the one include that fixes it by specification.

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

By the end of this guide
  • The error: the include is missing
  • The textbook version fails too - the common headers do not 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::count is a function template defined in the header <algorithm>. It returns the number of elements in the range [first, last) that are equal to the given value, comparing each element with operator==.

Because the template lives in one specific header, 'count' 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 never prints a note naming <algorithm>. In lean contexts it prints no suggestion at all; in some other contexts it suggests a different name entirely - popcount or cout - so the fix has to come from knowing the name's home, not from following a suggestion.

What makes this error hard to dodge is that almost no common header rescues it: <iostream>, <vector>, and <string> do not pull the name in on this library version, so even an ordinary file that counts occurrences in a vector and prints the result fails without <algorithm>. Of the headers probed here, only <functional> rescues it. Each case is separated below.

The error: the include is missing

This program counts how many times 2 occurs in the array {1, 2, 2, 3} with std::count, but includes only <type_traits>:

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 a_type_traits.cpp -o a_prog
a_type_traits.cpp: In function 'int main()':
a_type_traits.cpp:4:19: error: 'count' is not a member of 'std'
    4 |     auto n = std::count(a, a + 4, 2);
      |                   ^~~~~

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, 2, 3};
    auto n = std::count(a, a + 4, 2);
    return n == 2 ? 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:19: error: 'count' is not a member of 'std'
    3 |     auto n = std::count(a, a + 4, 2);
      |                   ^~~~~

The textbook version fails too - the common headers do not rescue it

The version most often written first - count the occurrences in a vector and print the count with std::cout - fails without <algorithm>:

C++
#include <iostream>
#include <vector>
int main() {
    std::vector<int> v = {1, 2, 2, 3, 2};
    auto n = std::count(v.begin(), v.end(), 2);
    std::cout << n << "\n";
    return 0;
}
Terminal
$ g++ -std=c++20 t_textbook.cpp -o t_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:5:19: error: 'count' is not a member of 'std'; did you mean 'cout'?
    5 |     auto n = std::count(v.begin(), v.end(), 2);
      |                   ^~~~~
      |                   cout

What happened: neither <iostream> nor <vector> pulls the name in transitively on this library version, so the call fails in a textbook file exactly as it does in a lean one. The suggestion here is a trap: GCC proposes cout, the output stream the file already uses, because the names look alike - following it would replace the counting call with something unrelated. Dropping <vector> changes nothing except the position - a file with only <iostream> that counts in a plain array gets the same error with the same cout suggestion. No header except <algorithm> is required to declare std::count, and on this library version the ordinary printing and container headers do not happen to do it either. A program that uses std::count includes <algorithm>.

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 counts the 2s in the array {1, 2, 2, 3}, the outcome splits three ways. These headers fail with the plain is not a member of 'std' error, with no note and no suggestion:

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

These headers fail too, but GCC adds a did you mean suggestion to the error line - popcount in each of these cases, a bit-counting name that has nothing to do with counting occurrences in a range:

  • <numeric>
  • <vector>
  • <string>
  • <array>

And with only <iostream>, the suggestion is cout, as in the textbook case above. A file with no include at all fails with the plain error. Of the headers probed here, only this one rescues the program - it compiles and runs with no <algorithm> anywhere in the file:

  • <functional>

That rescue is a property of this library version, not a rule of the language - no header except <algorithm> is required to declare std::count, and another library could leave it out of <functional> too.

This program includes only <utility>:

C++
#include <utility>
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 s_utility.cpp -o s_utility_prog
s_utility.cpp: In function 'int main()':
s_utility.cpp:4:19: error: 'count' is not a member of 'std'
    4 |     auto n = std::count(a, a + 4, 2);
      |                   ^~~~~

And this one includes only <numeric>, which is where the misleading suggestion appears:

C++
#include <numeric>
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 s_numeric.cpp -o s_numeric_prog
s_numeric.cpp: In function 'int main()':
s_numeric.cpp:4:19: error: 'count' is not a member of 'std'; did you mean 'popcount'?
    4 |     auto n = std::count(a, a + 4, 2);
      |                   ^~~~~
      |                   popcount

std::popcount counts set bits in an integer - it is not a spelling of the missing algorithm, and renaming the call to it does not fix the program. The suggestion appears because some name with a similar spelling happens to be declared in that context; it still never names <algorithm>.

The fix

Add the header:

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

One include, <algorithm>, declares the template. std::count(a, a + 5, 2) counts the elements equal to 2 in the array {1, 2, 2, 3, 2}, so the program prints 3.

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::count_if - the function template on the same reference page that counts the elements for which a predicate returns true - gets its own error, again with no header note and no suggestion:

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

std::find, the search 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, 3};
    auto it = std::find(a, a + 3, 2);
    return it != a + 3 && *it == 2 ? 0 : 1;
}
Terminal
$ g++ -std=c++20 c_find.cpp -o c_prog
c_find.cpp: In function 'int main()':
c_find.cpp:4:20: error: 'find' is not a member of 'std'
    4 |     auto it = std::find(a, a + 3, 2);
      |                    ^~~~

And std::sort, the sorting function in the same header, fails the same way:

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 d_sort.cpp -o d_prog
d_sort.cpp: In function 'int main()':
d_sort.cpp:4:10: error: 'sort' is not a member of 'std'
    4 |     std::sort(a, a + 3);
      |          ^~~~

So does std::fill, the function template that assigns one value to every element in a range:

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 e_fill.cpp -o e_prog
e_fill.cpp: In function 'int main()':
e_fill.cpp:4:10: error: 'fill' is not a member of 'std'
    4 |     std::fill(a, a + 3, 7);
      |          ^~~~

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

The neighboring names, fixed

With <algorithm> added, std::count works on more than a plain array. The vector {1, 2, 2, 3, 2} contains three 2s, and the string "veldica" contains one d:

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

And std::count_if with the predicate x > 1 counts four of the five elements in {1, 2, 2, 3, 4} - every element except the 1:

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

Since C++20 the same header also declares std::ranges::count, which takes the whole range instead of an iterator pair. With <algorithm> included it counts the 2s in the vector {1, 2, 2, 3, 2} and the program prints 3; in the lean context - only <type_traits> and <vector> included - it fails with its own wording, 'count' is not a member of 'std::ranges', because the missing include is the same one.

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, 2, 3};
    auto n = count(a, a + 4, 2);
    std::cout << n << "\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:14: error: 'count' was not declared in this scope
    5 |     auto n = count(a, a + 4, 2);
      |              ^~~~~
g_unqualified.cpp:5:14: 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:101:1: note:   'std::count'
  101 | count(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value);
      | ^~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:311:31: note:   'std::ranges::count'
  311 |   inline constexpr __count_fn count{};
      |                               ^~~~~

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::count; adding includes does not bring the name out of namespace std. Note that the std::count 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::count is declared by #include <algorithm>, and nowhere else by rule. In the sweep probed here, <type_traits>, <utility>, <numeric>, <limits>, <mutex>, <optional>, <cstring>, <vector>, <string>, <iostream>, <array>, and <cmath> do not declare it, and neither does a file with no include at all.
  • 'count' 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> under it. Some contexts add a did you mean suggestion instead: popcount with <numeric>, <vector>, <string>, or <array>, and cout with <iostream>. Neither suggested name is the fix.
  • In the sweep probed here, only <functional> rescues std::count transitively: a file whose only include is <functional> compiles and runs the call without <algorithm>. That is this library version's behavior, not a guarantee - no header is required to pull std::count in.
  • std::count_if, std::find, std::sort, and std::fill 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::count is declared by the same #include <algorithm> since C++20: with the include it counts in a whole range passed as one argument, and in the lean context probed here it gets the error 'count' is not a member of 'std::ranges' when that include is missing.
  • 'count' was not declared in this scope for an unqualified count use is a different defect: qualify the name as std::count - 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::count. 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 follow the did you mean suggestion when one appears. popcount counts set bits and cout is an output stream - neither counts occurrences in a range, and neither suggestion names the missing header.
  • Third, do not expect the textbook includes to save the file. In the sweep here <iostream>, <vector>, and <string> do not rescue the name - a file that counts and prints still needs <algorithm> itself.
  • Fourth, if the error says was not declared in this scope instead, fix the name's qualifier to std::count as well.
  • Fifth, if the failing name is a neighboring one - count_if, find, sort, or fill - 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::count used in a file whose only include is <type_traits>, <utility>, <limits>, <mutex>, <optional>, <cstring>, or <cmath>, or that includes nothing: the message is 'count' is not a member of 'std', with no header note and no suggestion - add #include <algorithm>.
  • std::count used with only <numeric>, <vector>, <string>, or <array> included: the same error, with did you mean 'popcount'? added - ignore the suggestion and add #include <algorithm>.
  • std::count used with only <iostream> included, or with <iostream> and <vector> in the textbook form: the same error, with did you mean 'cout'? added - ignore the suggestion and add #include <algorithm>.
  • std::count used with only <functional> included: no error on this library version - add #include <algorithm> anyway, because the rescue is not guaranteed.
  • std::count_if, std::find, std::sort, or std::fill used without <algorithm> in the lean context: the message names that name, with no header note - add #include <algorithm>.
  • count used without the std:: qualifier, <algorithm> included: the message is 'count' was not declared in this scope - qualify the name as std::count.

Checklist

  • Check the include list of the file that names the count, 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 follow a did you mean 'popcount' or did you mean 'cout' suggestion into a rename. The missing piece is an include, not a different name.
  • Do not be reassured by <iostream> or <vector> being present. In the sweep here neither rescues std::count at all - the missing <algorithm> is exposed in textbook files, not just lean ones.
  • 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 count - and std::count comes from <algorithm> in return.
  • After the fix, confirm the program runs, not just that it compiles: the fixed programs here print 3 for the array count, 3 and 1 for the vector and the string, 4 for the predicate count, and 3 for the ranges count.
  • '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 here, the ordinary container and printing headers do not rescue the name and only <functional> does.
  • 'std::max' Is Not a Member of 'std' Fix - the same missing-include shape for the greater-of-two function in <algorithm>; there, many common headers rescue the name transitively, here almost none do.

Sources

  • cppreference, std::count, std::count_if — for std::count and std::count_if being function templates defined in <algorithm>, std::count returning the number of elements in [first, last) equal to the given value, and std::count_if returning the number of elements for which the predicate returns true; both are constexpr since C++20.
  • cppreference, Standard library header <algorithm> — for <algorithm> being the header that declares the std::count, std::count_if, std::find, std::sort, and std::fill function templates.
  • cppreference, std::ranges::count, std::ranges::count_if — for std::ranges::count being an algorithm function object defined in <algorithm> since C++20 that counts the elements in a range equal to the given value, with an overload that takes the whole range as one argument.
  • 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).

Keep Exploring