1

I am trying to use multimap for the first time but my app will not compile. TIA Paul..

// file dept.h

typedef std::multimap <CString, std::map< CString, CString> > _DeparmentRecord;  // also tryied replacing CString with LPCWSTR

_DeparmentRecord DeparmentRecord;


// file dept.cpp

DWORD CIni::AddNameValue(LPCWSTR Section, LPCWSTR Name, LPCWSTR Value)
{

DeparmentRecord.insert(std::make_pair ( Section, std::make_pair(Name, Value)) );  <-- error here

}

c:\program files\microsoft visual studio 9.0\vc\include\utility(57) : error C2664: 'std::map<_Kty,_Ty>::map(const std::less<_Ty> &)' : cannot convert parameter 1 from 'const std::pair<_Ty1,_Ty2>' to 'const std::less<_Ty> &'

1> with 1> [ 1> _Kty=CString, 1> _Ty=CString 1> ] 1> and 1> [ 1> _Ty1=LPCWSTR, 1> _Ty2=LPCWSTR 1> ] 1> and 1> [ 1> _Ty=CString 1> ] 1> Reason: cannot convert from 'const std::pair<_Ty1,_Ty2>' to 'const std::less<_Ty>' 1> with 1> [ 1> _Ty1=LPCWSTR, 1> _Ty2=LPCWSTR 1> ] 1> and 1> [ 1> _Ty=CString 1> ] 1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called 1> c:\dev\projects\migrator\jobbuilder\jobbuilder\ini.cpp(55) : see reference to function template instantiation 'std::pair<_Ty1,_Ty2>::pair>(const std::pair> &)' being compiled 1> with 1> [ 1> _Ty1=const CString, 1> _Ty2=std::map 1> ]

========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

0

5 Answers 5

3

Change the function as follows.

DWORD AddNameValue(LPCWSTR  Section, LPCWSTR  Name, LPCWSTR  Value)
{
    std::map<CString, CString> aTemp;
    aTemp.insert(std::make_pair (Name, Value));
    DeparmentRecord.insert(std::make_pair (Section, aTemp)) ;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Note: you will replace the entry for Section, not update it.
But DepartmentRecord is a multimap. So wouldn't a new entry be added?
3

You're trying to insert a pair< section, pair<...> > into a map that takes pair< section, map<...> >.

Comments

2

std::make_pair(Name, Value) is a pair... but it should be a map.

STL errors can be a true pain. Using the very latest version of GCC can help, its error messages are much improved, but I see you're using MSVC so that's not much help to you.

1 Comment

Using the very latest MSVC helps too, you know.
1

In addition to the other answers, std::make_pair will return a std::pair. Don't expect the compiler to perform the conversion from LPCWSTR to CString for you.

Comments

1

In addition to the other correct answers, you'll avoid conversion problems (and likely get better error messages) if you use _DeparmentRecord's value_type typedef rather than make_pair.

DWORD AddNameValue(LPCWSTR Section, LPCWSTR Name, LPCWSTR Value)
{
  _DeparmentRecord::iterator i =
      DeparmentRecord.insert(_DeparmentRecord::value_type(Section, v)).first;
  i->second[Name] = Value;
}

And a small point: don't use leading underscores for non-local names - they're reserved for the standard library.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.