C++

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

std::map is a class template declared in the header , and the headers around container code do not declare it — not , not , and not . GCC helps here: its note names directly. The fix is to add the include the note points at.

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: <string>, <vector>, and <set> are not enough
  • The fix
  • Iteration prints the entries in sorted key order

Written November 27, 2026

Tested with GCC 13.3.0

What this guide is

std::map is a class template defined in the header <map>. It is a sorted associative container that holds key-value pairs with unique keys: the keys stay sorted by the comparison function, search, insertion, and removal all have logarithmic complexity, and iterating the container visits the entries in ascending order of keys. The same header also declares std::multimap, the variant that permits multiple entries with the same key.

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

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

The headers that sit near container code — <iostream>, <string>, <vector> — 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::map and stores a value in it, but includes only <iostream>:

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

The two errors after the note are knock-on effects of the first one: because the declaration of std::map was never seen, the compiler cannot parse the declaration of m, and the later use of m has nothing to refer to. The did you mean 'tm'? suggestion on the last error is a distraction — it points at a C time type that the standard headers happen to declare, and renaming anything to tm fixes nothing. 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>, <vector>, and <set> are not enough

Container code almost always already includes a string or container header, and those includes feel close enough. They are not. This program adds <string> and <vector> to the program above, and it fails the same way:

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

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

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

Two of those are worth pausing on. <set> is the closest relative in the list — a sorted container from the same family — and it still does not declare std::map. And <unordered_map> declares the hashed map, a different container in a different header; it does not declare the sorted 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::map on behalf of <map>, 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 <map> in every file that names std::map or std::multimap.

The fix

Add the header:

C++
#include <iostream>
#include <map>
#include <string>
int main() {
    std::map<std::string, int> scores;
    scores.insert({"alice", 3});
    scores["bob"] = 5;
    std::cout << scores.at("alice") << "\n";
    std::cout << scores["bob"] << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 good.cpp -o good_prog
$ ./good_prog
3
5

The same program also compiles and runs with no -std flag at all. insert adds the alice entry, scores["bob"] = 5 adds the bob entry by assignment, at reads the value stored under alice back, and the subscript read prints the value stored under bob. One include, <map>, declares the class template; <string> is there because the keys are strings, and it is the header that declares std::string.

Iteration prints the entries in sorted key order

A std::map keeps its keys sorted, so iteration visits the entries in ascending key order no matter what order they were inserted in. This program inserts three entries out of order and prints them all:

C++
#include <iostream>
#include <map>
#include <string>
int main() {
    std::map<std::string, int> scores;
    scores["carol"] = 7;
    scores["alice"] = 3;
    scores["bob"] = 5;
    for (const auto& entry : scores) {
        std::cout << entry.first << "=" << entry.second << "\n";
    }
    return 0;
}
Terminal
$ g++ -std=c++17 iterate.cpp -o iterate_prog
$ ./iterate_prog
alice=3
bob=5
carol=7

Each entry is a key-value pair: entry.first is the key and entry.second is the mapped value. The insertion order — carol, alice, bob — is gone from the output; the container's order is the sorted order of the keys.

operator[] inserts a missing key with a default value

Reading a key with the subscript operator does more than look the key up: if the key is not present, operator[] inserts it with a default-constructed value and returns a reference to that new entry. This program reads a key that was never inserted, and the size of the map changes because of the read:

C++
#include <iostream>
#include <map>
#include <string>
int main() {
    std::map<std::string, int> counts;
    std::cout << counts.size() << "\n";
    std::cout << counts["new"] << "\n";
    std::cout << counts.size() << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 subscript.cpp -o subscript_prog
$ ./subscript_prog
0
0
1

The map starts empty, the read of "new" prints the default value 0 for an int, and the size is 1 afterward — the read itself inserted the entry. That behavior is what makes counts[word]++ counting loops work, and it is also why a lookup that must not change the map uses at, find, or count instead of the subscript.

Looking up and erasing: count, find, and erase

The lookup members test and fetch entries without inserting anything. count returns how many entries carry a key — for a std::map, whose keys are unique, that is 1 or 0 — find returns an iterator to the entry or the end() iterator when the key is absent, and erase by key removes the entry and returns how many were removed:

C++
#include <iostream>
#include <map>
#include <string>
int main() {
    std::map<std::string, int> scores{{"alice", 3}, {"bob", 5}, {"carol", 7}};
    std::cout << scores.count("bob") << "\n";
    auto it = scores.find("carol");
    std::cout << it->second << "\n";
    std::cout << (scores.find("dave") == scores.end() ? "not found" : "found") << "\n";
    scores.erase("bob");
    std::cout << scores.size() << "\n";
    std::cout << scores.count("bob") << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 lookup.cpp -o lookup_prog
$ ./lookup_prog
1
7
not found
2
0

The find("dave") test compares the returned iterator against end() before touching it — dereferencing the end() iterator of a failed lookup is a defect, and the comparison is the standard guard. After erase("bob"), the size drops from 3 to 2 and count("bob") reports 0.

The neighboring errors: std::multimap and std::unordered_map

The same missing include is worded for whichever map name the program uses. With std::multimap — the sorted map that permits multiple entries with the same key, also defined in <map> — the error names that class instead, with the same note:

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

Because std::multimap lives in the same header, the fix is the same include. With <map> added, a multimap keeps both entries inserted under one key, where a std::map would keep only one:

C++
#include <iostream>
#include <map>
#include <string>
int main() {
    std::multimap<std::string, int> scores;
    scores.insert({"alice", 3});
    scores.insert({"alice", 4});
    std::cout << scores.count("alice") << "\n";
    for (const auto& entry : scores) {
        std::cout << entry.first << "=" << entry.second << "\n";
    }
    return 0;
}
Terminal
$ g++ -std=c++17 multimap_good.cpp -o multimap_good_prog
$ ./multimap_good_prog
2
alice=3
alice=4

std::unordered_map is the different case. It is the hashed associative container, defined in its own header <unordered_map> since C++11, and its elements are not sorted in any particular order. Without that header, the error names the class and the note names the other header:

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

Adding <map> does not fix this program, and adding <unordered_map> does not fix a std::map program — each container has its own header, and the note under each error names the right one. With <unordered_map> included, the program above stores and reads its value normally: a version that prints m.at(1) and m.count(2) prints 10 and 0.

A different wording: map without the std::

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

C++
#include <iostream>
#include <map>
int main() {
    map<int, int> m;
    m[1] = 10;
    std::cout << m[1] << "\n";
    return 0;
}
Terminal
$ g++ -std=c++17 unqualified.cpp -o unqualified_prog
unqualified.cpp: In function 'int main()':
unqualified.cpp:4:5: error: 'map' was not declared in this scope
    4 |     map<int, int> m;
      |     ^~~
unqualified.cpp:4:5: note: suggested alternatives:
In file included from /usr/include/c++/13/map:63,
                 from unqualified.cpp:2:
/usr/include/c++/13/bits/stl_map.h:102:11: note:   'std::map'
  102 |     class map
      |           ^~~
/usr/include/c++/13/map:80:13: note:   'std::pmr::map'
   80 |       using map
      |             ^~~
unqualified.cpp:4:9: error: expected primary-expression before 'int'
    4 |     map<int, int> m;
      |         ^~~
unqualified.cpp:5:5: error: 'm' was not declared in this scope; did you mean 'tm'?
    5 |     m[1] = 10;
      |     ^
      |     tm

'map' 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 <map>, so the include is not the problem — the declaration it adds is std::map, inside namespace std, and the use must be qualified as std::map, or the file must bring the name in deliberately. The notes under the error list the suggested alternatives: std::map itself, and std::pmr::map, the polymorphic-allocator alias also declared by <map>. 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::map and std::multimap are declared by #include <map>, and nowhere else by rule. <iostream>, <string>, <vector>, <unordered_map>, <utility>, <algorithm>, and <set> do not declare std::map.
  • 'map' is not a member of 'std' means the <map> include is missing, and GCC's note names the header directly: 'std::map' is defined in header '<map>'; did you forget to '#include <map>'? Follow the note.
  • The expected primary-expression before 'int' and 'm' was not declared in this scope errors that follow are knock-on effects of the missing declaration, not separate defects — and the did you mean 'tm'? suggestion on the second one is not a fix for anything.
  • std::unordered_map is declared by #include <unordered_map>, not by <map>: 'unordered_map' is not a member of 'std' takes the other header, and neither header substitutes for the other.
  • 'map' was not declared in this scope for an unqualified map(...) use is a different defect: qualify the name as std::map — 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::map. If <map> 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-m error are consequences, not causes.
  • Third, if the error says was not declared in this scope instead, fix the name's qualifier to std::map as well.
  • Fourth, if the failing name is std::unordered_map, add <unordered_map> instead — the sorted and hashed maps live in different headers, and the note under the error says which one the program needs.

Which fix for which cause

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

Checklist

  • Check the include list of the file that names the map, 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::map' is defined in header '<map>' — so take the fix from the note instead of guessing.
  • Do not rely on a neighboring header pulling std::map in. None of <iostream>, <string>, <vector>, <unordered_map>, <utility>, <algorithm>, or <set> does on this toolchain, and no header is required to.
  • If the program's keys 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 3 followed by 5, the sorted lines alice=3, bob=5, and carol=7, the sizes 0, 0, and 1 around a default-inserting read, and the lookup results 1, 7, not found, 2, and 0.
  • If duplicate keys are genuinely needed, switch to std::multimap — same header, same fix, different key rule. If sorted order is not needed, std::unordered_map is the hashed alternative — different header, <unordered_map>.

Sources

  • cppreference, std::map — for std::map being defined in <map>, holding key-value pairs with unique keys sorted by the comparison function, search, insertion, and removal having logarithmic complexity, iteration visiting entries in ascending key order, and operator[] accessing or inserting the element for a key.
  • cppreference, Standard library header <map> — for <map> declaring both the std::map and std::multimap class templates.
  • cppreference, std::multimap — for std::multimap being defined in <map>, holding a sorted list of key-value pairs while permitting multiple entries with the same key, and count returning the number of elements matching a key.
  • cppreference, std::unordered_map — for std::unordered_map being defined in <unordered_map> since C++11, holding key-value pairs with unique keys, and its elements not being sorted in any particular order.

Keep Exploring