C++
'sort_heap' Is Not a Member of 'std' Fix
std::sort_heap lives in one header, <algorithm> - and on this library version almost no ordinary file rescues it by accident: in the sweep probed here, covering all 106 standard headers in the library's header set, only <functional>, <chrono>, <execution>, <queue>, <regex>, and <valarray> pull the name in on the side, and the textbook combination of <iostream> and <vector> does not. The error therefore surfaces in textbook files as well as lean ones, and in the sweep probed here it takes one shape - the plain error, with no suggestion tail behind any probed header - or a transitive rescue. This guide pins down both outcomes, which probed headers cause each one, and the one include that fixes the error by specification.
Written December 1, 2026 · Tested with GCC 13.3.0 · 23 min read
- The error: the include is missing, in a lean file
- The textbook version fails too - plainly
- Neighboring headers: which ones fail plainly, which ones rescue it
- The fix
Written December 1, 2026
Tested with GCC 13.3.0
What this guide is
std::sort_heap is a function template defined in the header <algorithm>. It converts the heap in the range [first, last) into a sorted range: with the plain overload the heap is a max heap ordered by operator< (since C++20, by std::less{}), so the result is the same elements sorted in ascending order, and the comparator overload sorts with respect to the comparison the program passes instead. The heap property is no longer maintained afterward - after the call the range is sorted, not a heap. It returns nothing. Both overloads are constexpr since C++20. Its iterators must be random-access iterators, and it makes at most 2N log(N) comparisons, where N is the distance from first to last. There is no execution-policy overload of std::sort_heap - like the other heap algorithms, it was not given a parallel overload, and the fixed-programs section below shows what passing a policy does instead. Building the heap in the first place is a separate step: std::make_heap has its own guide and is not re-taught here, and removing the heap's largest element with std::pop_heap has its own guide too.
Because the template lives in one specific header, 'sort_heap' 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 never prints a note naming<algorithm>, and it never prints note lines, help lines, or a suggestion tail under the error - behind every failing sole header in the sweep the diagnostic is the plain error line and its caret block, nothing more. The neighboring names probed here -std::push_heap,std::pop_heap,std::is_heap,std::is_heap_until, andstd::make_heap, all in the same header - get no suggestion either in the same lean context, as the neighboring-names section shows.
What makes this error worth separating from its relatives is the shape of the sweep behind it. The sweep probed here 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 two ways - the plain compile error or a transitive rescue, with no suggestion class at all. The rescue side is the heap-family pattern measured for this name in its own sweep: the five headers that rescue the sorting neighbors, plus <queue>, whose std::priority_queue is built on the heap algorithms. The older siblings in this family - std::make_heap, std::push_heap, std::pop_heap, std::partial_sort, std::nth_element, std::rotate, std::partition, and std::sort - have their own guides and are linked at the end, not re-taught here.
The error: the include is missing, in a lean file
This program sorts a heap held in an array with std::sort_heap and returns success if the range comes out sorted ascending, but includes only <limits>:
#include <limits>
int main() {
int a[] = {6, 5, 4, 2, 3, 1};
std::sort_heap(a, a + 6);
return a[0] == 1 ? 0 : 1;
}
$ g++ -std=c++20 a_limits.cpp -o a_limits_prog
a_limits.cpp: In function 'int main()':
a_limits.cpp:4:10: error: 'sort_heap' is not a member of 'std'
4 | std::sort_heap(a, a + 6);
| ^~~~~~~~~
That is the whole output: one error, no suggestion on the error line, no note lines, no help lines. The same program with no include at all fails the same way:
int main() {
int a[] = {6, 5, 4, 2, 3, 1};
std::sort_heap(a, a + 6);
return a[0] == 1 ? 0 : 1;
}
$ g++ -std=c++20 n_none.cpp -o n_none_prog
n_none.cpp: In function 'int main()':
n_none.cpp:3:10: error: 'sort_heap' is not a member of 'std'
3 | std::sort_heap(a, a + 6);
| ^~~~~~~~~
The textbook version fails too - plainly
The version most often written first - sort a heap over a vector and print its smallest element from the front of the range, with std::cout - gets the same error, without <algorithm>:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {6, 5, 4, 2, 3, 1};
std::sort_heap(v.begin(), v.end());
std::cout << v.front() << "\n";
return 0;
}
$ g++ -std=c++20 t_textbook.cpp -o t_textbook_prog
t_textbook.cpp: In function 'int main()':
t_textbook.cpp:5:10: error: 'sort_heap' is not a member of 'std'
5 | std::sort_heap(v.begin(), v.end());
| ^~~~~~~~~
What happened: on this library version, neither <vector> nor <iostream> pulls this name in transitively, so the call fails in a textbook file just as it does in a lean one - and here the error is plain, with no suggestion tail at all. In the sweep probed here, the only headers that rescue std::sort_heap are <functional>, <chrono>, <execution>, <queue>, <regex>, and <valarray>, besides <algorithm> itself, and that rescue is a property of this library version, not a rule of the language. A program that uses std::sort_heap includes <algorithm>, whether or not it happens to compile without it.
Neighboring headers: which ones fail plainly, which ones 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 sorts a heap over an array and returns whether the range came out sorted ascending, 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 99 headers fail with the plain is not a member of 'std' error, with no suggestion, no note, and no help:
<any><array><atomic><barrier><bit><bitset><cassert><ccomplex><cctype><cerrno><cfenv><cfloat><charconv><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><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 plain way. No failing sole header adds a suggestion tail - the suggestion class for this name is empty.
A sole <vector> file shows the plain shape in full:
#include <vector>
int main() {
int a[] = {6, 5, 4, 2, 3, 1};
std::sort_heap(a, a + 6);
return a[0] == 1 ? 0 : 1;
}
$ g++ -std=c++20 v_vector.cpp -o v_vector_prog
v_vector.cpp: In function 'int main()':
v_vector.cpp:4:10: error: 'sort_heap' is not a member of 'std'
4 | std::sort_heap(a, a + 6);
| ^~~~~~~~~
And these headers rescue the program - it compiles and runs with no <algorithm> anywhere in the file:
<algorithm><chrono><execution><functional><queue><regex><valarray>
Two contrasts with the neighboring sweeps are worth holding on to. First, there is no link-failure shape in this sweep: every failing sole header fails at compile time with the error above, so the declared-but-not-defined link failure that std::swap produces behind five headers has no counterpart for this name in the probed set. Second, the rescue set carries one header the sorting neighbors do not: <queue>. Its class template std::priority_queue is implemented on top of the heap algorithms, and on this library version including it pulls std::sort_heap in on the side - still a transitive accident, not a declaration to rely on. <algorithm> - the home header - is on the rescue list by definition, not by transitivity, and every other rescue on that list is this library version's behavior, not a guarantee.
The fix
Add the header:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 4, 2, 3, 6};
std::make_heap(v.begin(), v.end());
std::sort_heap(v.begin(), v.end());
for (std::size_t i = 0; i < v.size(); ++i) {
if (i) std::cout << " ";
std::cout << v[i];
}
std::cout << "\n";
return 0;
}
$ g++ -std=c++20 h_fixed.cpp -o h_fixed_prog
$ ./h_fixed_prog
1 2 3 4 5 6
One include, <algorithm>, declares the template - and it declares std::make_heap too, which the program needs first: it builds a heap over 5, 1, 4, 2, 3, 6, then std::sort_heap turns that heap into a sorted range, and the program prints the full contents, 1 2 3 4 5 6.
The fixed programs: the sorted order, checked element by element
The sort leaves one thing to check: every element is at most the one after it, across the whole range. This program checks that element-by-element pass alongside std::is_sorted over the range, and reports 1, alongside the smallest element at the front and the largest at the back:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 4, 2, 3, 6};
std::make_heap(v.begin(), v.end());
std::sort_heap(v.begin(), v.end());
bool ok = std::is_sorted(v.begin(), v.end());
for (std::size_t i = 1; i < v.size(); ++i) ok = ok && (v[i - 1] <= v[i]);
std::cout << ok << " " << v.front() << " " << v.back() << "\n";
return 0;
}
$ g++ -std=c++20 k_values.cpp -o k_values_prog
$ ./k_values_prog
1 1 6
The comparator overload orders by the comparison the program passes instead: with std::greater<int>() from <functional>, the heap is a min heap, and sorting it with the same comparator leaves the range in descending order - std::is_sorted under the same comparator confirms it, and the program prints 1 6 1, the check, the largest value now at the front, and the smallest at the back:
#include <algorithm>
#include <functional>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 4, 2, 3, 6};
std::make_heap(v.begin(), v.end(), std::greater<int>());
std::sort_heap(v.begin(), v.end(), std::greater<int>());
bool ok = std::is_sorted(v.begin(), v.end(), std::greater<int>());
std::cout << ok << " " << v.front() << " " << v.back() << "\n";
return 0;
}
$ g++ -std=c++20 j_comp.cpp -o j_comp_prog
$ ./j_comp_prog
1 6 1
There is no execution-policy version to run. Passing std::execution::par as a first argument does not select a parallel overload, because none exists - the call fails with no matching function for call to 'sort_heap', and the notes list the only two candidates there are, the plain overload and the comparator overload:
#include <algorithm>
#include <execution>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {6, 5, 4, 2, 3, 1};
std::sort_heap(std::execution::par, v.begin(), v.end());
std::cout << v.front() << "\n";
return 0;
}
$ g++ -std=c++20 j_policy.cpp -o j_policy_prog
j_policy.cpp: In function 'int main()':
j_policy.cpp:7:19: error: no matching function for call to 'sort_heap(const __pstl::execution::v1::parallel_policy&, std::vector<int>::iterator, std::vector<int>::iterator)'
7 | std::sort_heap(std::execution::par, v.begin(), v.end());
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/13/bits/stl_algo.h:61,
from /usr/include/c++/13/algorithm:61,
from j_policy.cpp:1:
/usr/include/c++/13/bits/stl_heap.h:440:5: note: candidate: 'template<class _RAIter> constexpr void std::sort_heap(_RAIter, _RAIter)'
440 | sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last)
| ^~~~~~~~~
/usr/include/c++/13/bits/stl_heap.h:440:5: note: template argument deduction/substitution failed:
j_policy.cpp:7:19: note: deduced conflicting types for parameter '_RAIter' ('__pstl::execution::v1::parallel_policy' and '__gnu_cxx::__normal_iterator<int*, std::vector<int> >')
7 | std::sort_heap(std::execution::par, v.begin(), v.end());
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/13/bits/stl_heap.h:468:5: note: candidate: 'template<class _RAIter, class _Compare> constexpr void std::sort_heap(_RAIter, _RAIter, _Compare)'
468 | sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
| ^~~~~~~~~
/usr/include/c++/13/bits/stl_heap.h:468:5: note: template argument deduction/substitution failed:
j_policy.cpp:7:19: note: deduced conflicting types for parameter '_RAIter' ('__pstl::execution::v1::parallel_policy' and '__gnu_cxx::__normal_iterator<int*, std::vector<int> >')
7 | std::sort_heap(std::execution::par, v.begin(), v.end());
| ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
So a heap is sorted with the plain or comparator call, in the program's own thread; there is no policy argument to add, and <execution> is needed only for code that uses a policy with an algorithm that actually takes one.
The neighboring names: same header, plain in the lean context
The names that std::sort_heap is built alongside share its home header. In the lean context here - only <limits> included - they get no suggestion at all. std::push_heap - the function template defined in <algorithm> that inserts the element at the end of a range into the heap in front of it - gets its own error, again with no header note. Its own guide covers its error and its fix in full; it is shown here only as a neighbor:
#include <limits>
int main() {
int a[] = {6, 3, 5, 2, 1, 4, 7};
std::push_heap(a, a + 7);
return a[0] == 7 ? 0 : 1;
}
$ g++ -std=c++20 b_push.cpp -o b_push_prog
b_push.cpp: In function 'int main()':
b_push.cpp:4:10: error: 'push_heap' is not a member of 'std'
4 | std::push_heap(a, a + 7);
| ^~~~~~~~~
std::pop_heap - the function template defined in <algorithm> that removes the first element from a heap by moving it to the back of the range - fails the same way. Its own guide covers its error and its fix in full; it is shown here only as a neighbor:
#include <limits>
int main() {
int a[] = {6, 5, 4, 2, 3, 1};
std::pop_heap(a, a + 6);
return a[5] == 6 ? 0 : 1;
}
$ g++ -std=c++20 c_pop.cpp -o c_pop_prog
c_pop.cpp: In function 'int main()':
c_pop.cpp:4:10: error: 'pop_heap' is not a member of 'std'
4 | std::pop_heap(a, a + 6);
| ^~~~~~~~
std::is_heap - the function template defined in <algorithm> that reports whether a range is a heap - fails the same way:
#include <limits>
int main() {
int a[] = {6, 5, 4, 2, 3, 1};
return std::is_heap(a, a + 6) ? 0 : 1;
}
$ g++ -std=c++20 d_isheap.cpp -o d_isheap_prog
d_isheap.cpp: In function 'int main()':
d_isheap.cpp:4:17: error: 'is_heap' is not a member of 'std'
4 | return std::is_heap(a, a + 6) ? 0 : 1;
| ^~~~~~~
std::is_heap_until - the function template defined in <algorithm> that finds the largest subrange that is a heap - fails the same way:
#include <limits>
int main() {
int a[] = {6, 5, 4, 2, 3, 1};
return std::is_heap_until(a, a + 6) == a + 6 ? 0 : 1;
}
$ g++ -std=c++20 f_isheap_until.cpp -o f_isheap_until_prog
f_isheap_until.cpp: In function 'int main()':
f_isheap_until.cpp:4:17: error: 'is_heap_until' is not a member of 'std'
4 | return std::is_heap_until(a, a + 6) == a + 6 ? 0 : 1;
| ^~~~~~~~~~~~~
And std::make_heap - the function template defined in <algorithm> that constructs a heap out of a range - fails the same way, again with no header note. Its own guide covers its error and its fix in full; it is shown here only as a neighbor:
#include <limits>
int main() {
int a[] = {5, 1, 4, 2, 3, 6};
std::make_heap(a, a + 6);
return a[0] == 6 ? 0 : 1;
}
$ g++ -std=c++20 e_makeheap.cpp -o e_makeheap_prog
e_makeheap.cpp: In function 'int main()':
e_makeheap.cpp:4:10: error: 'make_heap' is not a member of 'std'
4 | std::make_heap(a, a + 6);
| ^~~~~~~~~
One contrast is worth holding on to: in the lean programs here, sort_heap, push_heap, pop_heap, is_heap, is_heap_until, and make_heap all get no note and no suggestion. Do not read the absence of a suggestion as a puzzle - in this probed set the fix comes from the names' home, <algorithm>, either way.
The neighboring names, fixed
And the neighboring names work under the same one added header: building a heap and confirming it with std::is_heap gives 1, pushing 7 puts 7 at the front, popping the heap moves that 7 to the back, and sorting the remaining heap leaves the range sorted ascending, 1 first and 6 last:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 4, 2, 3, 6};
std::make_heap(v.begin(), v.end());
bool h = std::is_heap(v.begin(), v.end());
v.push_back(7); std::push_heap(v.begin(), v.end());
int top = v[0];
std::pop_heap(v.begin(), v.end());
int popped = v.back(); v.pop_back();
std::sort_heap(v.begin(), v.end());
std::cout << h << " " << top << " " << popped << " " << v[0] << " " << v[5] << "\n";
return 0;
}
$ g++ -std=c++20 k_neighbors_fixed.cpp -o k_neighbors_fixed_prog
$ ./k_neighbors_fixed_prog
1 7 7 1 6
Since C++20 the same <algorithm> header also declares std::ranges::sort_heap, the ranges form of the same sort operation. It comes in two call shapes - an iterator pair, and a single range - and returns the past-the-end iterator of the range: here both shapes sort the heap ascending and return the end iterator, printing 1 6 1 1 6 1. In the lean context - only <limits> included - it fails with its own wording, 'std::ranges' has not been declared, because the namespace itself is not declared there. Behind a sole <vector> the wording changes: the namespace exists there, but the name is missing from it, so the error is 'sort_heap' is not a member of 'std::ranges':
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {6, 5, 4, 2, 3, 1};
std::vector<int> w = {6, 5, 4, 2, 3, 1};
auto e1 = std::ranges::sort_heap(v.begin(), v.end());
auto e2 = std::ranges::sort_heap(w);
std::cout << v[0] << " " << v[5] << " " << (e1 == v.end()) << " " << w[0] << " " << w[5] << " " << (e2 == w.end()) << "\n";
return 0;
}
$ g++ -std=c++20 r_ranges.cpp -o r_ranges_prog
$ ./r_ranges_prog
1 6 1 1 6 1
#include <limits>
int main() {
int a[] = {6, 5, 4, 2, 3, 1};
std::ranges::sort_heap(a, a + 6);
return a[0] == 1 ? 0 : 1;
}
$ g++ -std=c++20 r2_ranges_lean.cpp -o r2_ranges_lean_prog
r2_ranges_lean.cpp: In function 'int main()':
r2_ranges_lean.cpp:4:10: error: 'std::ranges' has not been declared
4 | std::ranges::sort_heap(a, a + 6);
| ^~~~~~
#include <vector>
int main() {
std::vector<int> v = {6, 5, 4, 2, 3, 1};
std::ranges::sort_heap(v.begin(), v.end());
return v[0] == 1 ? 0 : 1;
}
$ g++ -std=c++20 r3_ranges_vector.cpp -o r3_ranges_vector_prog
r3_ranges_vector.cpp: In function 'int main()':
r3_ranges_vector.cpp:4:18: error: 'sort_heap' is not a member of 'std::ranges'
4 | std::ranges::sort_heap(v.begin(), v.end());
| ^~~~~~~~~
A different wording - and a case where the qualifier is not needed
Including <algorithm> is not the only half of the name - but for this function the unqualified form behaves two ways, and which one a program gets depends on its arguments. This program includes the header, drops the std:: qualifier, and passes raw pointers into a C array:
#include <algorithm>
int main() {
int a[] = {6, 5, 4, 2, 3, 1};
sort_heap(a, a + 6);
return a[0] == 1 ? 0 : 1;
}
$ g++ -std=c++20 g2_unqualified_ptr.cpp -o g2_unqualified_ptr_prog
g2_unqualified_ptr.cpp: In function 'int main()':
g2_unqualified_ptr.cpp:4:5: error: 'sort_heap' was not declared in this scope
4 | sort_heap(a, a + 6);
| ^~~~~~~~~
g2_unqualified_ptr.cpp:4:5: note: suggested alternatives:
In file included from /usr/include/c++/13/bits/stl_algo.h:61,
from /usr/include/c++/13/algorithm:61,
from g2_unqualified_ptr.cpp:1:
/usr/include/c++/13/bits/stl_heap.h:468:5: note: 'std::sort_heap'
468 | sort_heap(_RandomAccessIterator __first, _RandomAccessIterator __last,
| ^~~~~~~~~
In file included from /usr/include/c++/13/algorithm:63:
/usr/include/c++/13/bits/ranges_algo.h:1706:35: note: 'std::ranges::sort_heap'
1706 | inline constexpr __sort_heap_fn sort_heap{};
| ^~~~~~~~~
That wording - was not declared in this scope - is a different defect with a different fix: the header is present, the qualifier is not, and raw pointers belong to no namespace, so no lookup finds the name. Here the notes do name the right names - the first points at std::sort_heap itself, shown at its comparator declaration in the internal header the transcript names, and the second at std::ranges::sort_heap. Qualify the name as std::sort_heap; adding includes does not bring the name out of namespace std.
The same unqualified call with std::vector iterators as its arguments behaves differently - it compiles and runs, printing 1 6, the smallest element at the front of the sorted range and the largest at the back:
#include <algorithm>
#include <iostream>
#include <vector>
int main() {
std::vector<int> v = {5, 1, 4, 2, 3, 6};
std::make_heap(v.begin(), v.end());
sort_heap(v.begin(), v.end());
std::cout << v.front() << " " << v.back() << "\n";
return 0;
}
$ g++ -std=c++20 g_unqualified.cpp -o g_unqualified_prog
$ ./g_unqualified_prog
1 6
What happened: the iterators belong to namespace std, so argument-dependent lookup finds std::sort_heap from the arguments themselves, even without the qualifier. That is a property of the argument types, not a style to rely on: the same call over raw pointers fails, as the transcript above shows. Write the qualifier either way.
The exact rule
std::sort_heapis declared by#include <algorithm>, and by no other header by rule. In the sweep probed here,, , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , - ,
, 'sort_heap' is not a member of 'std'means the<algorithm>include is missing and nothing else in the file pulled the name in. In the contexts probed here, GCC prints no note naming<algorithm>, no note lines, no help lines, and no suggestion tail under it - the suggestion class in the probed set is empty.- In the sweep probed here, the split is two-way: a sole header either fails plainly or rescues the program. No probed sole header produces a declared-but-not-defined link-failure shape for this name.
- In the sweep probed here, only six headers besides
<algorithm>rescuestd::sort_heaptransitively -<functional>,<chrono>,<execution>,<queue>,<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, those headers are not required to pullstd::sort_heapin. std::sort_heaphas only the plain and comparator overloads, bothconstexprsince C++20, and no execution-policy overload: passing a policy fails withno matching function for call to 'sort_heap', with the two existing overloads listed as the candidates.std::push_heap,std::pop_heap,std::is_heap,std::is_heap_until, andstd::make_heapare declared by the same#include <algorithm>: in the programs shown here, which include only<limits>beside the use, each gets its ownis not a member of 'std'error when that include is missing, with no header note and no suggestion.std::ranges::sort_heapis declared by the same#include <algorithm>since C++20: with the include it sorts the heap and returns the past-the-end iterator, in both its iterator-pair and its single-range call shapes; in the lean context probed here it gets the error'std::ranges' has not been declaredwhen that include is missing, and behind a sole<vector>it gets'sort_heap' is not a member of 'std::ranges'.'sort_heap' was not declared in this scopefor an unqualifiedsort_heapuse over raw pointers is a different defect: qualify the name asstd::sort_heap- the include alone does not bring the name out of namespacestd. The same unqualified use overstd::vectoriterators compiles in the program probed here through argument-dependent lookup; qualify the name there too, so the call does not depend on its argument types.
The fixes, ranked
- First, check the include list of the file that names
std::sort_heap. If<algorithm>is not there, add it - that is the fix in the common case, and it holds whether the file failed plainly or happened to compile before through a transitive rescue. - Second, do not wait for a suggestion or a note naming the header. For this error, in the contexts probed here, GCC prints neither - take the fix from the name's home header,
<algorithm>, instead. - Third, do not reach for
<utility>because the error sentence looks like thestd::swaperror. In the sweep probed here,<utility>as the only include fails plainly with the same error - the two names live in different headers, and each takes its own. - Fourth, do not be reassured by
<iostream>,<vector>, or<string>being present - in the sweep here none of them rescues this name at all - and do not remove<algorithm>because a file compiles without it: in the probed set only<functional>,<chrono>,<execution>,<queue>,<regex>, and<valarray>rescue it, and that rescue is not guaranteed. - Fifth, do not add an execution policy to the call. There is no policy overload of
std::sort_heapto select - the call fails withno matching function for call to 'sort_heap'- so sort the heap with the plain or comparator overload. - Sixth, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::sort_heapas well - and do not rely on an unqualified call that happens to compile overstditerators, because the same call over raw pointers fails. - Seventh, if the failing name is a neighboring one, check its header before fixing:
push_heap,pop_heap,is_heap,is_heap_until, andmake_heaptake the same added<algorithm>, whileswaptakes<utility>.
Which fix for which cause
#include <algorithm>missing,std::sort_heapused in a file whose only include is one of the 99 headers listed as plain in the sweep section above, or that includes nothing: the message is'sort_heap' is not a member of 'std', with no suggestion, no header note, and no help - add#include <algorithm>.std::sort_heapused in a textbook file including<iostream>and<vector>: the message is the same error, plainly, with no suggestion tail - add#include <algorithm>.std::sort_heapused with only<algorithm>,<functional>,<chrono>,<execution>,<queue>,<regex>, or<valarray>included: no error on this library version - add#include <algorithm>anyway when it is not the include already present, because the rescue is not guaranteed.std::sort_heapcalled with an execution policy as its first argument,<algorithm>included: the message isno matching function for call to 'sort_heap'- remove the policy; only the plain and comparator overloads exist.std::push_heap,std::pop_heap,std::is_heap,std::is_heap_until, orstd::make_heapused without<algorithm>in the lean context: the message names that name, plainly, with no header note - add#include <algorithm>.std::ranges::sort_heapused without<algorithm>in the lean context: the message is'std::ranges' has not been declared- add#include <algorithm>. Behind a sole<vector>the message is'sort_heap' is not a member of 'std::ranges'- the same fix.sort_heapused without thestd::qualifier,<algorithm>included, over raw pointers: the message is'sort_heap' was not declared in this scope- qualify the name asstd::sort_heap. Overstd::vectoriterators the same unqualified call compiles through argument-dependent lookup in the program probed here - qualify it anyway.
Checklist
- Check the include list of the file that names the sort, 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 suggestion or a note naming the header. For this error, in the contexts probed here, GCC prints no suggestion tail and no note naming
<algorithm>- take the fix from the name's home header,<algorithm>, instead. - Do not import the
std::swapfix into this error: in the probed set,<utility>fails here too, plainly. Split neighboring names by home header first. - 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>and<vector>do not rescuestd::sort_heapat all, and the six headers that do rescue it do so only transitively. - One header per name the file uses:
std::vectorcomes from<vector>andstd::stringcomes from<string>, even in a program whose subject is the heap - andstd::sort_heapcomes from<algorithm>in return. The comparator overload'sstd::greatercomes from<functional>, and there is no execution-policy overload to take<execution>for. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print
1 2 3 4 5 6for the full contents after the sort, and1 1 6for the sorted-order check over the range and the elements it found at the front and the back.
Related guides
- 'std::make_heap' Is Not a Member of 'std' Fix - the same missing-include shape for the heap-construction function template in
<algorithm>: its own full header sweep, its error, and its fix are covered there, and this guide links to it instead of re-teaching heap construction. - 'std::push_heap' Is Not a Member of 'std' Fix - the same missing-include shape for the heap-insertion function template in
<algorithm>: its own full header sweep, its error, and its fix are covered there. - 'std::pop_heap' Is Not a Member of 'std' Fix - the same missing-include shape for the heap-removal function template in
<algorithm>, the operation that moves a heap's largest element to the back one pop at a time where this guide's sort does the whole range at once: its own full header sweep, its error, and its fix are covered there. - 'std::partial_sort' Is Not a Member of 'std' Fix - the same missing-include shape for the partial sorting function template in
<algorithm>: its full header sweep splits three ways with a suggestion tail behind 2 failing headers, where this guide's sweep has no suggestion class at all. - 'std::sort' Is Not a Member of 'std' Fix - the same missing-include shape for the full sorting function template in
<algorithm>, an older sibling in this family whose own guide covers its error and its fix.
Sources
- cppreference,
std::sort_heap— forstd::sort_heapbeing a function template defined in<algorithm>that converts the heap in[first, last)into a sorted range, sorted with respect tooperator<(until C++20) /std::less{}(since C++20) for the plain overload and a comparator overload, bothconstexprsince C++20, the heap property no longer maintained after the call, at most 2N log(N) comparisons, returns none, and no execution-policy overload listed. - cppreference, Standard library header
<algorithm>— for<algorithm>being the header that declaresstd::sort_heapand the neighboring algorithm names, including thestd::ranges::sort_heapalgorithm function object since C++20. - cppreference,
std::ranges::sort_heap— forstd::ranges::sort_heapbeing an algorithm function object defined in<algorithm>since C++20,constexpr, constrained to random-access iterators and ranges withstd::sortable, in an iterator-sentinel form and a range form, that converts a max heap into a sorted range and returns the past-the-end iterator of the range, at most 2N log(N) applications of the comparison and twice as many applications of the projection. - cppreference,
std::make_heap— forstd::make_heapbeing a function template defined in<algorithm>that constructs a heap out of a range. - cppreference,
std::push_heap— forstd::push_heapbeing a function template defined in<algorithm>that inserts an element into a heap. - cppreference,
std::pop_heap— forstd::pop_heapbeing a function template defined in<algorithm>that removes the first element from a heap. - cppreference,
std::is_heap— forstd::is_heapbeing a function template defined in<algorithm>that checks whether a range is a heap. - cppreference,
std::is_heap_until— forstd::is_heap_untilbeing a function template defined in<algorithm>that finds the largest subrange that is a heap.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.