【问题标题】:Cannot convert std::pair with toolset v120 (Visual Studio)无法使用工具集 v120 (Visual Studio) 转换 std::pair
【发布时间】:2021-10-19 20:12:15
【问题描述】:

我在使用工具集 v120 时遇到问题,无法编译我的代码,但它适用于 v140。我的 IntelliSense 说:

IntelliSense:没有重载函数实例“std::map<_kty _ty _pr _alloc>::insert [with _Kty=std::string, _Ty=SHOPPURCHASE, _Pr=std::less<: string>, _Alloc=std::allocator<:pair std::string shoppurchase>>]" 匹配参数列表

参数类型有:(std::pair<:cstringt strtraitmfc_dll atl::chtraitscrt>>>, SHOPPURCHASE>)

BOOL SCHARDATA2::SETSHOPPURCHASE ( VECSHOP &vecSHOP )
{
    m_mapCharged.clear();
    DWORD dwSIZE = static_cast<DWORD>(vecSHOP.size());
    for ( DWORD i=0; i<dwSIZE; ++i )
    {
        const SHOPPURCHASE &sPURCHASE = vecSHOP[i];
        m_mapCharged.insert ( std::make_pair(sPURCHASE.strPurKey,sPURCHASE) );
    }

    return TRUE;
}

【问题讨论】:

    标签: c++ visual-studio visual-c++ msbuild


    【解决方案1】:

    错误是告诉您您正在尝试插入一个包含CString 的std::pair,其中应该是std::string。

    CString 不能隐式转换为std::string,因此您需要在它们之间进行显式转换。试试这个:

    m_mapCharged.insert ( std::make_pair(std::string(sPURCHASE.strPurKey.c_str()), sPURCHASE) );
    

    或者:您可以使用operator[] 代替insert():

    m_mapCharged[sPURCHASE.strPurKey.c_str()] = sPURCHASE;
    

    【讨论】:

      猜你喜欢
      • 2017-07-28
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      相关资源
      最近更新 更多