views:

333

answers:

5

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 ==========

+2  A: 

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

xtofl
+2  A: 

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.

Dietrich Epp
Using the very latest MSVC helps too, you know.
Dave Van den Eynde
And what about STLFilt
xtofl
+1  A: 

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.

Dave Van den Eynde
+3  A: 

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)) ;
}
Shino C G
Note: you will _replace_ the entry for Section, not update it.
xtofl
But DepartmentRecord is a multimap. So wouldn't a new entry be added?
AngryWhenHungry
A: 

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.

James Hopkin