c++windowswindows-runtimecppwinrt

Error related to headers and namespaces when migrating from C++/CX to C++/WinRT


File.h:

std::map<winrt::hstring, winrt::hstring> someMap;

File.cpp

auto it = someMap.find(someKey);
if (it != someMap.end()) {
    it.second += (winrt::hstring{L", "} + someString.c_str());
}

I get the following error:

'second': is not a member of 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>'
    with
    [
        _Ty=std::pair<winrt::hstring,winrt::hstring>
    ]
C:\Program Files (x86)\Microsoft Visual Studio\2017\Enterprise\VC\Tools\MSVC\14.16.27023\include\xtree(778): note: see declaration of 'std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<_Ty>>>'
    with
    [
        _Ty=std::pair<winrt::hstring,winrt::hstring>
    ]

I know from here that one needs to include the headers of every namespace we consume. I am assuming this error is from there and maybe that's why Visual Studio is not able resolve to the find from std::map and instead maps to the find from xtree.h. But I might be wrong. I did try including std as a namespace, but that doesnt seem to work or atleast it seems like I might need something in addition to that. What headers and/or namespaces should I include for this error to be resolved.


Solution

  • std::map::find returns an iterator. An iterator doesn't have first or second members, unlike the actual items of the map. If you want to access the item, you need to dereference the iterator, either by using the * or the -> operators:

    auto it = someMap.find(someKey);
    if (it != someMap.end()) {
        it->second += (winrt::hstring{L", "} + someString.c_str());
    }