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

Written November 27, 2026 · Tested with GCC 13.3.0 · 11 min read

By the end of this guide
  • The error: the include is missing
  • The same error through neighboring headers: <map> and <unorderedset> are not enough
  • The fix
  • Duplicates collapse, iteration is sorted: contains, count, and erase

Written November 27, 2026

Tested with GCC 13.3.0

What this guide is

std::set is a class template defined in the header <set>. It is an associative container that contains a sorted set of unique objects: each value can be present at most once, iteration visits the values in sorted order, and search, insertion, and removal operations have logarithmic complexity.

Because the class template lives in one specific header, 'set' is not a member of 'std' has one common cause:

  • The #include <set> line is missing. For this error GCC does name the header: its note says 'std::set' is defined in header '<set>'; did you forget to '#include <set>'?, and following that note is the whole fix.

The headers that sit near set-valued code — <iostream>, <map>, <unordered_set> — 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::set, inserts a value into it, and reads the size back, but includes only <iostream>:

C++
#include <iostream>
int main() {
    std::set<int> s;
    s.insert(42);
    std::cout << s.size() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 noinclude.cpp -o noinclude_prog
noinclude.cpp: In function 'int main()':
noinclude.cpp:3:10: error: 'set' is not a member of 'std'
    3 |     std::set<int> s;
      |          ^~~
noinclude.cpp:2:1: note: 'std::set' is defined in header '<set>'; did you forget to '#include <set>'?
    1 | #include <iostream>
  +++ |+#include <set>
    2 | int main() {
noinclude.cpp:3:14: error: expected primary-expression before 'int'
    3 |     std::set<int> s;
      |              ^~~
noinclude.cpp:4:5: error: 's' was not declared in this scope
    4 |     s.insert(42);
      |     ^

The two errors after the note are knock-on effects of the first one: because the declaration of std::set was never seen, the compiler cannot parse the declaration of s, and the later use of s has nothing to refer to. 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: <map> and <unordered_set> are not enough

Code that works with sets almost always already includes a map or hashed-set header, and those includes feel close enough. They are not. This program adds <map> and <unordered_set> to the program above, and it fails the same way:

C++
#include <iostream>
#include <map>
#include <unordered_set>
int main() {
    std::set<int> s;
    s.insert(42);
    std::cout << s.size() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 combined.cpp -o combined_prog
combined.cpp: In function 'int main()':
combined.cpp:5:10: error: 'set' is not a member of 'std'
    5 |     std::set<int> s;
      |          ^~~
combined.cpp:4:1: note: 'std::set' is defined in header '<set>'; did you forget to '#include <set>'?
    3 | #include <unordered_set>
  +++ |+#include <set>
    4 | int main() {
combined.cpp:5:14: error: expected primary-expression before 'int'
    5 |     std::set<int> s;
      |              ^~~
combined.cpp:6:5: error: 's' was not declared in this scope
    6 |     s.insert(42);
      |     ^

The same result holds for the other neighboring headers tested, each with the same error and the same note naming <set>:

  • With only <iostream> included, the program fails with 'set' is not a member of 'std'.
  • With only <map> and <iostream> included, the program fails with 'set' is not a member of 'std'.
  • With only <vector> and <iostream> included, the program fails with 'set' is not a member of 'std'.
  • With only <string> and <iostream> included, the program fails with 'set' is not a member of 'std'.
  • With only <unordered_set> and <iostream> included, the program fails with 'set' is not a member of 'std'.
  • With only <algorithm> and <iostream> included, the program fails with 'set' is not a member of 'std'.
  • With only <utility> and <iostream> included, the program fails with 'set' is not a member of 'std'.
  • With only <memory> and <iostream> included, the program fails with 'set' is not a member of 'std'.

Two of those are worth pausing on. <map> declares the sorted key-value container std::map, the closest relative std::set has, and <unordered_set> declares the hashed set std::unordered_set — both still do not declare std::set. 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::set on behalf of <set>, 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 <set> in every file that names std::set.

The fix

Add the header:

C++
#include <iostream>
#include <set>
int main() {
    std::set<int> s;
    s.insert(42);
    std::cout << s.size() << "\n";
    std::cout << *s.begin() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 good.cpp -o good_prog
$ ./good_prog
1
42

The same program also compiles and runs with no -std flag at all. After the insert the set holds one element, so size prints 1, and dereferencing begin prints the stored value, 42. One include, <set>, declares the class template.

Duplicates collapse, iteration is sorted: contains, count, and erase

A set keeps each value at most once and keeps its values in sorted order, and the lookup and removal operations are the other half of using the type. contains reports whether a value is present, count returns the number of elements matching a key — for a set, 0 or 1 — and erase removes a value by key:

C++
#include <iostream>
#include <set>
int main() {
    std::set<int> s;
    s.insert(5);
    s.insert(1);
    s.insert(3);
    s.insert(1);
    s.insert(5);
    std::cout << s.size() << "\n";
    for (int v : s) {
        std::cout << v << "\n";
    }
    std::cout << s.contains(3) << "\n";
    std::cout << s.count(4) << "\n";
    s.erase(1);
    std::cout << s.size() << "\n";
    for (int v : s) {
        std::cout << v << "\n";
    }
    return 0;
}
Terminal
$ g++ -std=c++20 behavior.cpp -o behavior_prog
$ ./behavior_prog
3
1
3
5
1
0
2
3
5

Five inserts produce a size of 3: the repeated 1 and 5 are already present, so those inserts change nothing. Iteration prints the values in sorted order — 1, 3, 5 — not insertion order. contains(3) prints 1 because 3 is present, and count(4) prints 0 because 4 is not. After erase(1) the size is 2 and iteration prints 3 and 5. One version note: contains is a C++20 member — under -std=c++17 this same program stops at 'class std::set<int>' has no member named 'contains', while count works in both modes — so the program above is compiled with -std=c++20.

The neighboring error: std::multiset fails the same way, in the same header

std::multiset is the sorted container that allows multiple keys with equivalent values, and it is defined in the same header <set> as std::set. Without that header, the failure is worded exactly like the std::set case, with the same kind of note naming <set>:

C++
#include <iostream>
int main() {
    std::multiset<int> m;
    m.insert(2);
    std::cout << m.size() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 multiset_noinclude.cpp -o multiset_noinclude_prog
multiset_noinclude.cpp: In function 'int main()':
multiset_noinclude.cpp:3:10: error: 'multiset' is not a member of 'std'
    3 |     std::multiset<int> m;
      |          ^~~~~~~~
multiset_noinclude.cpp:2:1: note: 'std::multiset' is defined in header '<set>'; did you forget to '#include <set>'?
    1 | #include <iostream>
  +++ |+#include <set>
    2 | int main() {
multiset_noinclude.cpp:3:19: error: expected primary-expression before 'int'
    3 |     std::multiset<int> m;
      |                   ^~~
multiset_noinclude.cpp:4:5: error: 'm' was not declared in this scope; did you mean 'tm'?
    4 |     m.insert(2);
      |     ^
      |     tm

The did you mean 'tm'? tail on the last error is a knock-on suggestion about the undeclared variable, not about the container — the container fix is still the note's include. Because the two class templates share a header, the one include fixes both. With <set> added, the same program keeps the duplicate values that a std::set would collapse:

C++
#include <iostream>
#include <set>
int main() {
    std::multiset<int> m;
    m.insert(2);
    m.insert(2);
    m.insert(1);
    std::cout << m.size() << "\n";
    std::cout << m.count(2) << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 multiset_good.cpp -o multiset_good_prog
$ ./multiset_good_prog
3
2

Three inserts give a size of 3 — the second 2 is kept, where a std::set would have held two elements — and count(2) prints 2. If every value must be unique, that is the std::set case above; if repeated values are part of the data, that is the std::multiset case — and both need the same include. The hashed counterpart is a separate case again: std::unordered_set is defined in its own header <unordered_set>, and without it the error is 'unordered_set' is not a member of 'std' with a note naming <unordered_set> — the same shape, a different header.

A different wording: set without the std::

Calling the class without its namespace qualifier is a different defect, and GCC words it differently:

C++
#include <iostream>
#include <set>
int main() {
    set<int> s;
    s.insert(42);
    std::cout << s.size() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 unqualified.cpp -o unqualified_prog
unqualified.cpp: In function 'int main()':
unqualified.cpp:4:5: error: 'set' was not declared in this scope
    4 |     set<int> s;
      |     ^~~
unqualified.cpp:4:5: note: suggested alternatives:
In file included from /usr/include/c++/13/set:63,
                 from unqualified.cpp:2:
/usr/include/c++/13/bits/stl_set.h:96:11: note:   'std::set'
   96 |     class set
      |           ^~~
/usr/include/c++/13/set:80:13: note:   'std::pmr::set'
   80 |       using set = std::set<_Key, _Cmp, polymorphic_allocator<_Key>>;
      |             ^~~
unqualified.cpp:4:9: error: expected primary-expression before 'int'
    4 |     set<int> s;
      |         ^~~
unqualified.cpp:5:5: error: 's' was not declared in this scope
    5 |     s.insert(42);
      |     ^

'set' 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 <set>, so the include is not the problem — the declaration it adds is std::set, inside namespace std, and the use must be qualified as std::set, or the file must bring the name in deliberately. GCC's notes under the error list the suggested alternatives — std::set first, then the std::pmr::set alias — and the first note points at the declaration itself. 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::set is declared by #include <set>, and nowhere else by rule. <iostream>, <map>, <vector>, <string>, <unordered_set>, <algorithm>, <utility>, and <memory> do not declare std::set.
  • 'set' is not a member of 'std' means the <set> include is missing, and GCC's note names the header directly: 'std::set' is defined in header '<set>'; did you forget to '#include <set>'? Follow the note.
  • The expected primary-expression before 'int' and 's' was not declared in this scope errors that follow are knock-on effects of the missing declaration, not separate defects.
  • std::multiset is declared by the same #include <set>: without it, its error is worded the same way with a note naming <set>, and the one include declares both class templates.
  • std::unordered_set is declared by #include <unordered_set>, not by <set>: its missing-include error has the same shape with a note naming <unordered_set>.
  • 'set' was not declared in this scope for an unqualified set(...) use is a different defect: qualify the name as std::set — 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::set. If <set> 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-s error are consequences, not causes.
  • Third, if the error says was not declared in this scope instead, fix the name's qualifier to std::set as well.
  • Fourth, if the failing name is std::multiset and its note also names <set>, the same added include fixes it — the two sorted containers share the header.

Which fix for which cause

  • #include <set> missing, std::set used with <iostream> present: the message is 'set' is not a member of 'std' — add #include <set>.
  • Only <map>, <vector>, <string>, <unordered_set>, <algorithm>, <utility>, or <memory> included: same message, same note — add #include <set>.
  • std::multiset used without <set>: the message is 'multiset' is not a member of 'std' with a note naming <set> — add #include <set>.
  • std::unordered_set used without <unordered_set>: the message is 'unordered_set' is not a member of 'std' with a note naming <unordered_set> — add #include <unordered_set>.
  • set(...) used without the std:: qualifier: the message is 'set' was not declared in this scope — qualify the name as std::set and include <set>.

Checklist

  • Check the include list of the file that names the set, 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::set' is defined in header '<set>' — so take the fix from the note instead of guessing.
  • Do not rely on a neighboring header pulling std::set in. None of <iostream>, <map>, <vector>, <string>, <unordered_set>, <algorithm>, <utility>, or <memory> does on this toolchain, and no header is required to.
  • If the set holds std::string values, 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 1 and 42 for a one-element set, and 3, 1, 3, 5, 1, 0, 2, 3, and 5 across the duplicate-insert, lookup, and erase sequence.
  • If repeated values must be kept rather than collapsed, std::multiset is the neighboring type — same header, <set>, and the same missing-include error shape when that header is missing.

Sources

  • cppreference, std::set — for std::set being defined in <set>, containing a sorted set of unique objects, search, insertion, and removal having logarithmic complexity, and insert, erase, count, and contains (since C++20) being its modifier and lookup operations.
  • cppreference, Standard library header <set> — for <set> declaring the std::set and std::multiset class templates.
  • cppreference, std::multiset — for std::multiset being defined in <set>, containing a sorted set of objects where multiple keys with equivalent values are allowed, unlike std::set.
  • cppreference, std::unordered_set — for std::unordered_set being defined in <unordered_set>, containing a set of unique objects organized into buckets by hash rather than sorted.

Keep Exploring