CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: map of pairs

  1. #1
    Join Date
    Aug 2005
    Location
    southampton, UK
    Posts
    1,320

    map of pairs

    i am having a problem trying to populate a map of pairs:
    Code:
    typedef std::pair<BotWindowCropArea*,BotWindowTestElements*> OCRComponent;
    std::map<int,OCRComponent> ocrcomponents;
    now when i try the following:
    Code:
    OCRComponent tmpocrcmp;
    ocrcomponents.insert(0,tmpocrcmp);
    i get the following compiler errors:
    Code:
    error C2439: 'std::pair<_Ty1,_Ty2>::first' : member could not be initialized with [ _Ty1=const int, _Ty2=OCRComponent ]
    error C2440: 'initializing' : cannot convert from 'BotWindowCropArea *const ' to 'const int'
    error C2664: 'std::pair<_Ty1,_Ty2>::pair(const std::pair<_Ty1,_Ty2> &)' : cannot convert parameter 1 from 'BotWindowTestElements *const ' to 'const std::pair<_Ty1,_Ty2> &'with[    _Ty1=BotWindowCropArea *,    _Ty2=BotWindowTestElements *]and[    _Ty1=BotWindowCropArea *,    _Ty2=BotWindowTestElements *]
    Anyone know why? Cheers!

  2. #2
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: map of pairs

    Try:
    Code:
    ocrcomponents.insert(std::map<int, OCRComponent>::value_type(0, ocrcomponents));
    std::map::insert expects a std:air<key, value> (which is the same as std::map<key, value>::value_type) parameter.
    There is also two-args version of std::map::insert... That's the version you used without knowing it. There is a two-args std::map::insert which takes a map iterator as first parameter, indicating the position where to insert. And there is a two-args std::map::insert which copies the data described by an aribtrary iterator range.
    Code:
    // inside class map
    
       pair<iterator, bool> insert (const value_type& x);
    
       iterator insert (iterator position, const value_type& x);
       template <class InputIterator>
    
        void insert (InputIterator first, InputIterator last);
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured