Why doesn't this compile? I am trying to use multimap and am unsuccessful.

#include <iostream>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
		multimap<string,long> test;
		string a="BLAH";
		long b=8;
		pair<string,long> c( a, b );
		test.insert( c );
		return 0;
}

CC Test.cpp -o Test
"Test.cpp", line 11: Error: Could not find a match for std::multimap<std::basic_string<char, std::char_traits<char>, std::allocator<char>>, long, std::less<std::basic_string<char, std::char_traits<char>, std::allocator<char>>>, std::allocator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char>>, long>>>::insert(std::pair<std::basic_string<char, std::char_traits<char>, std::allocator<char>>, long>).
1 Error(s) detected.

Dani AI

Generated

Short summary and cause

The error comes from the fact that an associative container’s element type is value_type, and for std::multimap that is std::pair<const Key, T>. insert expects a value_type (or something constructible to it), so some STL implementations accept a pair<Key,T> and convert it implicitly, but non‑conforming or older libraries may not. That mismatch is what the compiler message is reporting. (en.cppreference.com)

What to do (portable fixes)

  • Construct the exact value_type the container expects. For example:

    using VT = std::multimap<std::string,long>::value_type;
    test.insert(VT("BLAH", 8));
  • On modern compilers prefer in‑place construction with emplace, which forwards arguments to construct value_type directly:

    test.emplace("BLAH", 8);  // C++11 and later

Constructing the container’s value_type avoids any dependence on implicit conversions that some older STL implementations mishandle; emplace avoids conversion/copy altogether. (en.cppreference.com)

Why some replies worked and what to try next

As and noted, make_pair or constructing a pair will often work on conforming compilers; ’s later note that pair<const Key, T> fixed the problem is consistent with the root cause. If these fixes still fail, the problem is almost certainly your compiler / STL (Sun Forte 6 had several library conformance gaps). The practical options are: use the value_type / emplace approach above, switch to a different STL implementation or a newer compiler, or keep explicitly constructing pair<const Key,T> as a workaround. (stackoverflow.com)

Troubleshooting checklist

  • Ensure <string> and <map> are included (not the real cause here, but common).
  • Try the value_type or emplace snippets above.
  • If still failing, try compiling with a modern compiler (gcc/clang) or a newer Sun/Oracle release; the error text you posted is typical of a non‑conforming STL, not buggy user code. (en.cppreference.com)

Recommended Answers

All 8 Replies

#include <string>

Not the cause. I have the same problem with int:

#include <iostream>
#include <string>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
		multimap<int,int> test;
		int a=6;
		int b=8;
		test.insert( a,b );
		return 0;
}

CC Test.cpp -o Test
"Test.cpp", line 11: Error: Could not find a match for std::multimap<int, int, std::less<int>, std::allocator<std::pair<const int, int>>>::insert(int, int).
1 Error(s) detected.

test.insert( make_pair(a,b) );

Basically same error:

CC Test.cpp -o Test
"Test.cpp", line 11: Error: Could not find a match for std::multimap<int, int, std::less<int>, std::allocator<std::pair<const int, int>>>::insert(std::pair<int, int>).

test.insert(pair<int,int>(a,b));

That works for me..?

-Fredric

I think I'm coming to the conclusion that this is compiler specific...I'm using Sun Forte 6 (solaris)

its gotta be. If you include string in the first example, it should work. If you include functional in mine, it will work and also daishis example is valid too. You can get a different version of the stl and try that or maybe check for updates to your compiler because these codes mentioned are all standard compliant and should compile without fuss.

apparently this is a known issue with the version of the compiler I'm using - I followed this link here and was succesful. (I tried the first suggestion of making the pair<const int, int> instead of pair<int, int> . Haven't tried it with other datatypes yet.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.