C++
'std::vector' Is Not a Member of 'std' Fix
std::vector is a class template declared in the header
Written November 27, 2026 · Tested with GCC 13.3.0 · 10 min read
- The error: the include is missing
- The same error through neighboring headers: <string> and <algorithm> are not enough
- The fix
- Iteration visits the elements in insertion order
Written November 27, 2026
Tested with GCC 13.3.0
What this guide is
std::vector is a class template defined in the header <vector>. It is a sequence container that holds a dynamic array: the number of elements can change at run time, the elements are stored contiguously, and they keep the order they were put in. Values are usually added at the end with push_back, and size reports how many elements the vector currently holds.
Because the class template lives in one specific header, 'vector' is not a member of 'std' has one common cause:
- The
#include <vector>line is missing. For this error GCC does name the header: its note says'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?, and following that note is the whole fix.
The headers that sit near container code — <iostream>, <string>, <algorithm> — do not rescue the program. Each one is tested below, and each fails the same way.
The error: the include is missing
This program declares a std::vector and appends a value to it, but includes only <iostream>:
#include <iostream>
int main() {
std::vector<int> v;
v.push_back(10);
std::cout << v[0] << "\n";
return 0;
}
$ g++ -std=c++17 noinclude.cpp -o noinclude_prog
noinclude.cpp: In function 'int main()':
noinclude.cpp:3:10: error: 'vector' is not a member of 'std'
3 | std::vector<int> v;
| ^~~~~~
noinclude.cpp:2:1: note: 'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?
1 | #include <iostream>
+++ |+#include <vector>
2 | int main() {
noinclude.cpp:3:17: error: expected primary-expression before 'int'
3 | std::vector<int> v;
| ^~~
noinclude.cpp:4:5: error: 'v' was not declared in this scope
4 | v.push_back(10);
| ^
The two errors after the note are knock-on effects of the first one: because the declaration of std::vector was never seen, the compiler cannot parse the declaration of v, and the later use of v has nothing to refer to. Unlike the map case, there is no did you mean suggestion on the undeclared-v error here — the line stands on its own. The default compilation mode prints the identical error, so this shape is not a language-mode problem. Fix the first error — the missing include — and the other two disappear with it.
The same error through neighboring headers: <string> and <algorithm> are not enough
Container code almost always already includes a string or algorithm header, and those includes feel close enough. They are not. This program adds <string> and <algorithm> to the program above, and it fails the same way:
#include <iostream>
#include <string>
#include <algorithm>
int main() {
std::vector<int> v;
v.push_back(10);
std::cout << v[0] << "\n";
return 0;
}
$ g++ -std=c++17 combined.cpp -o combined_prog
combined.cpp: In function 'int main()':
combined.cpp:5:10: error: 'vector' is not a member of 'std'
5 | std::vector<int> v;
| ^~~~~~
combined.cpp:4:1: note: 'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?
3 | #include <algorithm>
+++ |+#include <vector>
4 | int main() {
combined.cpp:5:17: error: expected primary-expression before 'int'
5 | std::vector<int> v;
| ^~~
combined.cpp:6:5: error: 'v' was not declared in this scope
6 | v.push_back(10);
| ^
The same result holds for the other neighboring headers tested, each with the same error and the same note naming <vector>:
- With only
<iostream>included, the program fails with'vector' is not a member of 'std'. - With only
<string>and<iostream>included, the program fails with'vector' is not a member of 'std'. - With only
<algorithm>and<iostream>included, the program fails with'vector' is not a member of 'std'. - With only
<array>and<iostream>included, the program fails with'vector' is not a member of 'std'. - With only
<map>and<iostream>included, the program fails with'vector' is not a member of 'std'. - With only
<memory>and<iostream>included, the program fails with'vector' is not a member of 'std'. - With only
<utility>and<iostream>included, the program fails with'vector' is not a member of 'std'. - With only
<list>and<iostream>included, the program fails with'vector' is not a member of 'std'.
Two of those are worth pausing on. <map> and <list> are container headers themselves, and <array> is the fixed-size sequence container — all three still do not declare std::vector. And <algorithm> works on containers constantly, yet it does not declare this one. No neighboring header rescues this program on GCC 13.3.0 — there is no transitive-include exception here. No standard header is required to declare std::vector on behalf of <vector>, and a program that compiled only because some other header happened to pull the declaration in as a side effect could break under a different library version. Include <vector> in every file that names std::vector.
The fix
Add the header:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
std::cout << v.size() << "\n";
std::cout << v[0] << "\n";
std::cout << v[2] << "\n";
return 0;
}
$ g++ -std=c++17 good.cpp -o good_prog
$ ./good_prog
3
10
30
The same program also compiles and runs with no -std flag at all. Each push_back appends one value at the end, so after three calls size reports 3, the subscript read at index 0 prints the first value added, and the read at index 2 prints the last one. One include, <vector>, declares the class template.
Iteration visits the elements in insertion order
A std::vector keeps its elements in the order they were put in, so iteration visits them in that order — the opposite of a sorted container such as std::map. This program builds a vector from an initializer list in a scrambled order and prints every element:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{30, 10, 20};
for (int n : v) {
std::cout << n << "\n";
}
std::cout << v.size() << "\n";
return 0;
}
$ g++ -std=c++17 iterate.cpp -o iterate_prog
$ ./iterate_prog
30
10
20
3
Nothing is sorted or reordered: the output is 30, 10, 20, exactly as written, followed by the size, 3. If a program needs its values in sorted order, it has to sort the vector itself or use a container that keeps its keys sorted.
Reading elements: at and operator[]
Both at and the subscript operator read the element at an index, and at can also be assigned through. This program reads with both, writes through at, and prints the first and last elements with front and back:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{10, 20, 30};
std::cout << v.at(1) << "\n";
std::cout << v[2] << "\n";
v.at(0) = 99;
std::cout << v.front() << "\n";
std::cout << v.back() << "\n";
return 0;
}
$ g++ -std=c++17 access.cpp -o access_prog
$ ./access_prog
20
30
99
30
at(1) prints the second element, 20, and v[2] prints the third, 30. The assignment through at(0) replaces the first element, which is why front then prints 99, while back still prints the last element, 30. The difference between the two reads is in what happens outside the valid range, not in a read like this one: at is the checked access and the subscript is the unchecked one, so code that computes its index uses at when an out-of-range index is possible.
Shrinking and growing in place: pop_back and resize
A vector changes size after it is built, not only by appending. pop_back removes the last element, and resize sets the element count directly, filling any new elements with a value given for them:
#include <iostream>
#include <vector>
int main() {
std::vector<int> v{10, 20, 30};
v.pop_back();
std::cout << v.size() << "\n";
std::cout << v.back() << "\n";
v.resize(4, 7);
std::cout << v.size() << "\n";
std::cout << v[3] << "\n";
return 0;
}
$ g++ -std=c++17 resize_pop.cpp -o resize_pop_prog
$ ./resize_pop_prog
2
20
4
7
After pop_back, the size is 2 and the last element is 20 — the 30 is gone. resize(4, 7) then grows the vector back to 4 elements, and the element at index 3, one of the two added by the resize, holds the fill value 7.
The neighboring error: std::array fails with different wording
std::array is the fixed-size sequence container, defined in its own header <array>: its element count is part of its type and does not change. Without that header, the failure is worded differently from the std::vector case — there is no 'array' is not a member of 'std' line and no note naming a header:
#include <iostream>
int main() {
std::array<int, 3> a{10, 20, 30};
std::cout << a[1] << "\n";
return 0;
}
$ g++ -std=c++17 array_noinclude.cpp -o array_noinclude_prog
array_noinclude.cpp: In function 'int main()':
array_noinclude.cpp:3:24: error: variable 'std::array<int, 3> a' has initializer but incomplete type
3 | std::array<int, 3> a{10, 20, 30};
| ^
Something in the headers this program does include leaves the name std::array visible but incomplete, so GCC reports the variable's type as incomplete instead of reporting the name as missing — and because the error carries no header note, the fix has to come from knowing the container's header rather than from following a note. The fix is still one include. With <array> added, the same values store and read normally:
#include <array>
#include <iostream>
int main() {
std::array<int, 3> a{10, 20, 30};
std::cout << a.size() << "\n";
std::cout << a[1] << "\n";
return 0;
}
$ g++ -std=c++17 array_good.cpp -o array_good_prog
$ ./array_good_prog
3
20
The size is fixed at 3 by the type itself, and the subscript read prints the second element, 20. If the element count needs to change at run time, that is the std::vector case above, not this one — and adding <vector> does not fix an std::array program, just as adding <array> does not fix a std::vector program, as the header test above showed.
A different wording: vector without the std::
Calling the class without its namespace qualifier is a different defect, and GCC words it differently:
#include <iostream>
#include <vector>
int main() {
vector<int> v;
v.push_back(10);
std::cout << v[0] << "\n";
return 0;
}
$ g++ -std=c++17 unqualified.cpp -o unqualified_prog
unqualified.cpp: In function 'int main()':
unqualified.cpp:4:5: error: 'vector' was not declared in this scope
4 | vector<int> v;
| ^~~~~~
unqualified.cpp:4:5: note: suggested alternatives:
In file included from /usr/include/c++/13/vector:66,
from unqualified.cpp:2:
/usr/include/c++/13/bits/stl_vector.h:428:11: note: 'std::vector'
428 | class vector : protected _Vector_base<_Tp, _Alloc>
| ^~~~~~
/usr/include/c++/13/vector:86:13: note: 'std::pmr::vector'
86 | using vector = std::vector<_Tp, polymorphic_allocator<_Tp>>;
| ^~~~~~
unqualified.cpp:4:12: error: expected primary-expression before 'int'
4 | vector<int> v;
| ^~~
unqualified.cpp:5:5: error: 'v' was not declared in this scope
5 | v.push_back(10);
| ^
'vector' was not declared in this scope is the 'has not been declared' family for this topic: an unqualified name that lookup cannot find at all. This program already includes <vector>, so the include is not the problem — the declaration it adds is std::vector, inside namespace std, and the use must be qualified as std::vector, or the file must bring the name in deliberately. The notes under the error list the suggested alternatives: std::vector itself, and std::pmr::vector, the polymorphic-allocator alias also declared by <vector>. If your error says is not a member of 'std', the name was qualified and the include is the whole problem; if it says was not declared in this scope, check the qualifier first.
The exact rule
std::vectoris declared by#include <vector>, and nowhere else by rule.<iostream>,<string>,<algorithm>,<array>,<map>,<memory>,<utility>, and<list>do not declarestd::vector.'vector' is not a member of 'std'means the<vector>include is missing, and GCC's note names the header directly:'std::vector' is defined in header '<vector>'; did you forget to '#include <vector>'?Follow the note.- The
expected primary-expression before 'int'and'v' was not declared in this scopeerrors that follow are knock-on effects of the missing declaration, not separate defects. std::arrayis declared by#include <array>, not by<vector>: without<array>its error on this toolchain is an incomplete-type error with no header note, and neither header substitutes for the other.'vector' was not declared in this scopefor an unqualifiedvector(...)use is a different defect: qualify the name asstd::vector— the include alone does not bring the name out of namespacestd.
The fixes, ranked
- First, check the include list of the file that names
std::vector. If<vector>is not there, add it — that is the fix in the common case, and it is the fix GCC's own note names. - Second, do not chase the follow-on errors. Fix the include and recompile; the parse error and the undeclared-
verror are consequences, not causes. - Third, if the error says
was not declared in this scopeinstead, fix the name's qualifier tostd::vectoras well. - Fourth, if the failing name is
std::arrayand the error says its type is incomplete, add<array>instead — the dynamic and fixed-size sequence containers live in different headers, and this error prints no note to follow.
Which fix for which cause
#include <vector>missing,std::vectorused with<iostream>present: the message is'vector' is not a member of 'std'— add#include <vector>.- Only
<string>,<algorithm>,<array>,<map>,<memory>,<utility>, or<list>included: same message, same note — add#include <vector>. std::arrayused without<array>: the message isvariable 'std::array<int, 3> a' has initializer but incomplete type— add#include <array>.vector(...)used without thestd::qualifier: the message is'vector' was not declared in this scope— qualify the name asstd::vectorand include<vector>.
Checklist
- Check the include list of the file that names the vector, not just the project. The include must be in that file, or in a header that file includes directly by design.
- Read the note under the error. For this error GCC names the header —
'std::vector' is defined in header '<vector>'— so take the fix from the note instead of guessing. - Do not rely on a neighboring header pulling
std::vectorin. None of<iostream>,<string>,<algorithm>,<array>,<map>,<memory>,<utility>, or<list>does on this toolchain, and no header is required to. - If the program's elements are
std::string, that name comes from<string>— one header per name the file uses, even when a program happens to compile without it. - After the fix, confirm the program runs, not just that it compiles: the fixed programs here print the size and values
3,10, and30after three appends, the elements30,10, and20in insertion order, the reads20,30,99, and30, and the sizes and values2,20,4, and7around apop_backand aresize. - If the element count is fixed by the type and never changes,
std::arrayis the fixed-size alternative — different header,<array>, and a differently worded error when that header is missing.
Related guides
- 'std::map' Is Not a Member of 'std' Fix — the same missing-include shape for the sorted map in
<map>, whose iteration order is sorted by key where a vector's is insertion order. - 'std::unique_ptr' Is Not a Member of 'std' Fix — the same missing-include shape for the smart pointer in
<memory>, with the same note naming the header. - C++ STL Containers: Vector, Map, and Set — the wider tour of the containers this vector belongs with, including
std::map.
Sources
- cppreference,
std::vector— forstd::vectorbeing defined in<vector>, being a sequence container that holds a dynamic array with elements stored contiguously, andpush_back,size,at,front,back,pop_back, andresizebeing its element and size operations. - cppreference, Standard library header
<vector>— for<vector>declaring thestd::vectorclass template. - cppreference,
std::array— forstd::arraybeing defined in<array>, holding a fixed-size sequence of elements, with its size fixed by its type. - cppreference, Standard library header
<array>— for<array>declaring thestd::arrayclass template.
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.