C++

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

std::stack lives in one header, <stack>, and has since C++98 - there is no language-mode half to this error. Miss the header and the name is not a member of std, with a note naming the header to add; include it and the same program compiles in every standard mode. Among the headers probed in this guide's sweep, none rescues the name at all - not even <queue>, the sibling adapter header, or <deque>, the stack's default underlying container: the title error stays exactly as it was.

Written November 29, 2026 · Tested with GCC 13.3.0 · 15 min read

By the end of this guide
  • The rule underneath: one header, any standard mode
  • The failure: no &lt;stack&gt; include
  • The same failure at namespace scope: does not name a template type
  • No older-mode shape: &lt;stack&gt; included compiles at C++98

Written November 29, 2026

Tested with GCC 13.3.0

What this guide is

The error on a std::stack name means the compiler has no stack name inside std to find. Inside a function, GCC words it exactly as this guide's title promises: 'stack' is not a member of 'std'. The cause is a missing include: nothing in the file included <stack>, the header that declares the name, so the lookup fails at the first use. For this name that is the whole story - there is no second shape. std::stack dates from C++98, the first standard, so no language mode is too old for it: with the header included, the same program compiles at -std=c++98 and at -std=c++11 alike, as the included-header section below shows. When a std::stack program fails with this error, the mode is never the fault; the include is.

The wording changes with where the failing name sits. At namespace scope, where the name is used as a type for a variable declaration, GCC reports 'stack' in namespace 'std' does not name a template type instead - note the word "template": this name is a class template, and the namespace-scope wording says so, where the same fault on a non-template name is worded as "does not name a type". Both wordings appear below; they are one fault - the name is absent - read at two different positions in the file.

This error also carries its remedy with it: GCC attaches a note to the std::stack error naming the header to add - 'std::stack' is defined in header '<stack>'; did you forget to '#include <stack>'? Read the note before anything else in the cascade, because it is the diagnosis and the fix in one line. And among the headers probed in the sweep below, no neighboring header rescues this name at all - not even <queue>, which declares the sibling adapters, and not even <deque>, the stack's default underlying container: the title error persists unchanged in every sweep program. The fix stays direct: #include <stack> in the file itself.

The rule underneath: one header, any standard mode

The std::stack page defines the type: stack is a container adapter that gives the programmer the functionality of a stack - specifically, a LIFO (last-in, first-out) data structure. The class template acts as a wrapper to the underlying container, and the stack pushes and pops the element from the back of the underlying container, known as the top of the stack. By default, if no container class is specified for a stack instantiation, the standard container std::deque is used as that underlying container. The page's own history reaches back to C++98: its defect reports are applied to C++98, and the class template carries no later-standard date of its own (the dates on the page belong to later additions around it, such as emplace since C++11). Its member list gives the operations this guide's programs use: top accesses the top element, push inserts an element at the top, pop removes the top element, size returns the number of elements, and empty checks whether the adapter is empty - and pop is declared void pop(), returning nothing, which matters below.

The header page shows what the header declares, in namespace std: the stack class template - described there as adapting a container to provide a stack (LIFO data structure) - its comparison functions, and the std::swap(std::stack) overload. So a std::stack name needs exactly one condition: the header included, so the declarations are in the file. No mode condition stands next to it.

The failure: no <stack> include

This program uses std::stack<int> and includes no header at all - deliberately, because a companion header would be its own possible rescue, and the sweep below measures each candidate header on its own instead:

C++
int main() {
    std::stack<int> s;
    s.push(10);
    return s.top() == 10 ? 0 : 1;
}
Terminal
$ g++ -std=c++11 p1_fail_no_include.cpp -o p1_fail_no_include
p1_fail_no_include.cpp: In function 'int main()':
p1_fail_no_include.cpp:2:10: error: 'stack' is not a member of 'std'
    2 |     std::stack<int> s;
      |          ^~~~~
p1_fail_no_include.cpp:1:1: note: 'std::stack' is defined in header '<stack>'; did you forget to '#include <stack>'?
  +++ |+#include <stack>
    1 | int main() {
p1_fail_no_include.cpp:2:16: error: expected primary-expression before 'int'
    2 |     std::stack<int> s;
      |                ^~~
p1_fail_no_include.cpp:3:5: error: 's' was not declared in this scope
    3 |     s.push(10);
      |     ^

Read the note first: it sits directly under the title error and names the header, so this shape diagnoses itself - the include is missing, and the note says which one. The errors that follow are the cascade from that one absence. expected primary-expression before 'int' is the compiler trying to read std::stack<int> as something other than a template name, because the template name is not there. And 's' was not declared in this scope is fallout: the declaration failed, so the variable does not exist where it is used on the next line.

The same failure at namespace scope: does not name a template type

The same missing include at namespace scope is worded differently. This program declares the stack as a file-scope variable, still with no header:

C++
std::stack<int> g_s;

int main() {
    g_s.push(10);
    return g_s.top() == 10 ? 0 : 1;
}
Terminal
$ g++ -std=c++11 p1b_fail_scope.cpp -o p1b_fail_scope
p1b_fail_scope.cpp:1:6: error: 'stack' in namespace 'std' does not name a template type
    1 | std::stack<int> g_s;
      |      ^~~~~
p1b_fail_scope.cpp:1:1: note: 'std::stack' is defined in header '<stack>'; did you forget to '#include <stack>'?
  +++ |+#include <stack>
    1 | std::stack<int> g_s;
p1b_fail_scope.cpp: In function 'int main()':
p1b_fail_scope.cpp:4:5: error: 'g_s' was not declared in this scope
    4 |     g_s.push(10);
      |     ^~~

Same fault, different first line: where the name is used as a type outside a function, GCC says it does not name a template type; where it is used inside a function, GCC says it is not a member of std. The wording is worth recognizing exactly, because "does not name a template type" is this name's namespace-scope form - a non-template name in the same position is reported as "does not name a type", without the word "template". The header note appears here too, unchanged, naming <stack> as the remedy, and the undeclared-variable error follows inside main exactly as in the function-scope shape. Recognize either wording as the missing-include shape.

No older-mode shape: <stack> included compiles at C++98

Names that arrived in a later standard have a second failure shape - header included, mode too old - and guides for those names show a #error from inside the library headers as its first diagnostic. This name has no such shape, and the honest way to show that is to compile the included program in the oldest mode: it succeeds. This program includes <stack>, pushes two values, and prints the top and the size:

C++
#include <stack>
#include <iostream>

int main() {
    std::stack<int> s;
    s.push(10);
    s.push(20);
    std::cout << s.top() << " " << s.size() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++98 p2_included.cpp -o p2_cpp98

$ ./p2_cpp98
20 2
$ g++ -std=c++11 p2_included.cpp -o p2_cpp11

$ ./p2_cpp11
20 2

No diagnostics in either mode, and the same output, 20 2, from both binaries: the top of the stack is the last value pushed, 20, and the stack holds 2 elements. So if a std::stack program shows the title error, do not reach for the compile line - there is no mode in which <stack> refuses to declare the name, and no "only available from" note exists for it. The missing include is the only shape this error takes, which makes the diagnosis shorter than for the later-standard names: read the header note, add the include, and the error is gone.

The header sweep: no rescue among the headers probed

A missing standard name is sometimes rescued by accident, when another header happens to include the one that declares it. For std::stack the sweep in this guide finds no rescue at all among the headers probed. Each sweep program uses the stack with one candidate header as its only include, and no other header - a second include would be its own possible rescue and would spoil the measurement. This program, with <queue> in that position and still no <stack>, fails with exactly the missing-include shape:

C++
#include <queue>

int main() {
    std::stack<int> s;
    s.push(10);
    return s.top() == 10 ? 0 : 1;
}
Terminal
$ g++ -std=c++11 sweep_queue.cpp -o sweep_queue
sweep_queue.cpp: In function 'int main()':
sweep_queue.cpp:4:10: error: 'stack' is not a member of 'std'
    4 |     std::stack<int> s;
      |          ^~~~~
sweep_queue.cpp:2:1: note: 'std::stack' is defined in header '<stack>'; did you forget to '#include <stack>'?
    1 | #include <queue>
  +++ |+#include <stack>
    2 | 
sweep_queue.cpp:4:16: error: expected primary-expression before 'int'
    4 |     std::stack<int> s;
      |                ^~~
sweep_queue.cpp:5:5: error: 's' was not declared in this scope
    5 |     s.push(10);
      |     ^

Read what did not change. The title error is still there, word for word: the compiler still says stack is not a member of std, still attaches the same header note naming <stack>, and the same cascade follows. In the sweep above, no candidate header declared the name, even as a forward declaration - so there is no half-rescued incomplete-type state for this name among the headers probed, unlike names where a probed neighbor declares without defining. The same program was compiled with each of <vector>, <deque>, <list>, <string>, <iostream>, <utility> and <algorithm> as the only candidate header, at C++11: every one fails with the same title error, the same header note, and the same cascade shape. Two of those results are worth pausing on. <queue> declares the sibling adapters queue and priority_queue - the header a reader would most expect to leak this one - and it does not. And <deque> is the stack's default underlying container, the type every default stack is built on - and it does not leak the adapter's name either. So of the eight headers probed in place of <stack>, all eight leave the title error in place and none produces a working program - among the headers probed, the name simply does not arrive by accident. That makes the repair the direct one: include <stack> itself in every file that names std::stack.

The fix: include <stack>

The header included, and the adapter is fully usable in any standard mode. This program pushes three values onto a stack, reads the top back with top(), pops it, and prints the new top and the stack's size:

C++
#include <stack>
#include <iostream>

int main() {
    std::stack<int> s;
    s.push(10);
    s.push(20);
    s.push(30);
    std::cout << s.top() << "\n";
    s.pop();
    std::cout << s.top() << " size " << s.size() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++11 p4_fix.cpp -o p4_fix

$ ./p4_fix
30
20 size 2

Every name that failed before now resolves. The stack is built in three steps: push(10), push(20) and push(30) put the values on in that order, and a stack is last-in, first-out - so the top is the last value pushed, 30, which is what the first line prints. pop() then removes that top element without returning it, leaving 20 on top with 2 elements in the stack, which is what the second line prints. A stack exposes only its top - there is no way to read the values underneath without popping down to them - so every printed value here is fully determined by the values in the program, and any other output means the program, not the machine, differs.

Neighboring error: calling front() on a stack

Once <stack> is included, a different error appears when the stack is read the way a queue or a sequence container is read. A stack exposes only its top element, so it has no front() member - s.front() fails like this:

C++
#include <stack>

int main() {
    std::stack<int> s;
    s.push(10);
    return s.front() == 10 ? 0 : 1;
}
Terminal
$ g++ -std=c++11 p5_neighbour_front.cpp -o p5_neighbour_front
p5_neighbour_front.cpp: In function 'int main()':
p5_neighbour_front.cpp:6:14: error: 'class std::stack<int>' has no member named 'front'
    6 |     return s.front() == 10 ? 0 : 1;
      |              ^~~~~

The contrast separates the two errors cleanly. This error says nothing about a missing name in std - the include is already right, the type was found, and the failure is the member itself: there is no front on a stack to call. The title error names stack as not a member of std, because the type was never there to be used. The repair here is to change the access, not the include: the element front() would name on a queue is the element top() names on a stack - the most recently pushed value still on it - so read s.top() instead. The one element a stack lets you read is its top; the elements underneath are reachable only by popping.

Neighboring error: taking a value from pop()

The other neighboring fault also keeps the right include and expects the wrong thing from a member the adapter does provide. pop() removes the top element and returns nothing - it is declared void pop() - so using its result as a value fails like this:

C++
#include <stack>

int main() {
    std::stack<int> s;
    s.push(10);
    int x = s.pop();
    return x == 10 ? 0 : 1;
}
Terminal
$ g++ -std=c++11 p6_neighbour_pop_value.cpp -o p6_neighbour_pop_value
p6_neighbour_pop_value.cpp: In function 'int main()':
p6_neighbour_pop_value.cpp:6:18: error: void value not ignored as it ought to be
    6 |     int x = s.pop();
      |             ~~~~~^~

Nothing in this outcome says a name is missing - the include is right, the stack was built, and the failure is the expectation, not the name. The line to recognize is the one marked error:: void value not ignored as it ought to be, at the assignment, where the program tries to keep a value that the call does not produce. The repair is to split the two steps the single expression tried to fuse: read the value first with int x = s.top();, then remove it with s.pop(); - top() hands back the top element, and pop() then discards it. When a pop of a stack fails with a void-value error, read it as the two operations written as one - the adapter keeps them separate on purpose, so that reading the top and removing it are distinct steps.

Decision summary

  • The first errors are 'stack' is not a member of 'std', with a note under it saying 'std::stack' is defined in header '<stack>'; did you forget to '#include <stack>'?: the header is missing - add #include <stack>, exactly as the note says; every later error in the cascade traces to the absent name.
  • The first error is 'stack' in namespace 'std' does not name a template type, with the same header note under it: the same missing include, read at namespace scope where the name is used as a type - add <stack> the same way.
  • The file already includes <stack> and the program still fails: it is not this error and not a language-mode problem - with the header included, the same program compiles and runs at -std=c++98 and at -std=c++11; std::stack has no older-mode shape, so read the actual first error instead of changing the compile line.
  • The error is still the title error even though the file includes some other header: in the sweep above, each of <vector>, <deque>, <list>, <queue>, <string>, <iostream>, <utility> and <algorithm> in place of <stack> left the title error in place - among the headers probed, none declares this name by accident, so a neighboring include is not a state to reason about for this name; add <stack> itself.
  • The error instead is 'class std::stack<int>' has no member named 'front': the type was found and the access form is wrong for this adapter - a stack exposes only its top element and has no front(); read s.top() instead, and reach the elements underneath only by popping.
  • The error instead is void value not ignored as it ought to be at an assignment from s.pop(): the include is right and the expectation is wrong - pop() returns void; read the value with s.top() first, then call s.pop() to remove it.

Checklist

  • The file includes <stack> itself - not a neighboring header in its place; in the sweep above, each of the eight probed headers left the title error in place, so none of them stands in for <stack>.
  • No language-mode change is part of this fix: std::stack dates from C++98, and with <stack> included the program compiles in the oldest mode as well as the newest - if the title error appears, the include is the fault, never the mode.
  • The first error is read before the cascade: the expected primary-expression and 'was not declared in this scope' errors disappear when the name error at the top is fixed.
  • Elements are read with top() only - never front(), back() or a subscript: a stack exposes only its top element, and the elements underneath are reached by popping.
  • A value is never taken from pop(): pop() returns void - read top() first, then call pop() to remove what was read.
  • The fixed program compiles, runs, and prints 30 and 20 size 2, in order.
  • 'std::queue' Is Not a Member of 'std' Fix - the same missing-name fault on the sibling container adapter: std::queue lives in <queue> where std::stack lives in <stack> - and the sweep above puts the two side by side, because <queue> in place of <stack> did not rescue this name.
  • 'std::deque' Is Not a Member of 'std' Fix - the same missing-name fault on the stack's default underlying container: std::deque lives in <deque>, and <deque> in place of <stack> did not rescue this name either, even though every default stack is built on a deque.
  • 'std::unordered_map' Is Not a Member of 'std' Fix - the same missing-name fault with the opposite mode story: that name arrived in C++11 and has a genuine older-mode shape with a #error from inside the library headers, where std::stack, present since C++98, has none.

Sources

  • cppreference.com, std::stack — the type at the center of the error: the class template stack defined in header <stack>, a container adapter giving the functionality of a stack - a LIFO (last-in, first-out) data structure - wrapping an underlying container that is std::deque by default, pushing and popping from the back of that container, known as the top of the stack, and its member list giving top, push, pop, size and empty, the operations the fix and repair programs use.
  • cppreference.com, Standard library header — what the header declares: the stack class template, described as adapting a container to provide a stack (LIFO data structure), its comparison functions, and the std::swap(std::stack) overload, in namespace std - the declarations a file gets by including <stack> itself; in the sweep above, no probed neighbor brought them in.
  • cppreference.com, std::stack::pop — the member the pop neighboring error misreads: declared void pop(), it removes the top element from the stack and effectively calls pop_back() on the underlying container, returning nothing - the rule the neighboring error enforces, and the reason the repair reads top() before calling pop().

Keep Exploring