Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a pair of std::pair inside another std::pair?

Tags:

c++

templates

stl

I'm declaring a map of string to a pair of pairs as follow:

std::map<std::wstring, 
         std::pair<std::pair<long, long>, 
                   std::pair<long, long>>> reference;

And I initialize it as:

reference.insert(L"First", 
                 std::pair<std::pair<long, long>, 
                           std::pair<long, long>>(std::pair<long, long>(-1, -1),
                           std::pair<long, long>(0, 0)));

However, Visual C++ gives me the error "C2664, No constructor could take the source type, or constructor overload resolution was ambiguous".

I'm new to using templates and STL and I can't tell what I'm doing wrong.

like image 347
Fábio Avatar asked Sep 28 '10 14:09

Fábio


2 Answers

The >>> can not be parsed correctly (unless you have a C++0x compiler).

Change to > > >

This:

reference.insert("First",

Should be:

reference.insert(L"First",
                ^^^

Also there is a utility function to make the construction of pairs easier:

std::pair<std::pair<long, long>, std::pair<long, long>>(std::pair<long, long>(-1, -1), std::pair<long, long>(0, 0))

Can be:

std::make_pair(std::make_pair(-1L,-1L),std::make_pair(0L,0L))

Try this:

reference[L"First"]
    = std::make_pair(std::make_pair(-1L,-1L),std::make_pair(0L,0L));
like image 62
Martin York Avatar answered Oct 03 '22 07:10

Martin York


C++ gets confused by the consecutive ">" when you close the template as it interprets that as the shift operator.

Add spaces between the closing templates, change >>> to > > >

like image 45
iniju Avatar answered Oct 03 '22 08:10

iniju