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.

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.

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.