错误C2664:无法从'const std :: basic_string< _Elem,_Traits,_Ax>'转换参数1到'std :: wstring&

时间:2013-09-12 13:39:13

标签: c++

error C2664: 'CCertStoreHelper::DeleteCtl' : cannot convert parameter 1 from 'const std::basic_string<_Elem,_Traits,_Ax>' to 'std::wstring &error C2664: 'CCertStoreHelper::DeleteCtl' : cannot convert parameter 1 from 'const std::basic_string<_Elem,_Traits,_Ax>' to 'std::wstring &
with
      [
              _Elem=wchar_t,
              _Traits=std::char_traits<wchar_t>,
              _Ax=std::allocator<wchar_t>
      ]
      Conversion loses qualifiers

我不知道这个。所以请提供解决方案。

代码:

CCertStoreHelper certCaStore;
std::set<std::wstring> ctlIdentifiersToRemove; // It populates data which I m not mentioning


std::set<std::wstring>::iterator iter1;
std::set<std::wstring>::iterator iter2;

for(iter1 = ctlIdentifiersToRemove.begin(); iter1 != ctlIdentifiersToRemove.end(); iter1++)
{
    iter2 = ctlIdentifiersReferenced.find((*iter1));
    if(iter2 == ctlIdentifiersReferenced.end()) 
    {
        if(certCaStore.DeleteCtl((*iter1))) // error line
        {
            // ...
        }
    }
}
// prototype for DeleteCtl fun is
bool CCertStoreHelper::DeleteCtl(std::wstring &ctlIdentifier)

请告诉我我做错了什么 感谢

1 个答案:

答案 0 :(得分:2)

正如twalberg所指出的,编译器错误消息中最重要的一点是“丢失限定符”位。它还告诉您,它无法从const std::wstring转换为std::wstring&,只是它将第一个std::wstring扩展为完整的模板实例化表单。

问题是你的DeleteCtl通过非const引用接受参数,好像它想要在那里修改字符串(坏主意),但它不能这样做,因为你正在迭代一个集合,一旦他们在那里就不能更改集合的成员(std::setconst_iteratoriterator之间没有区别。原因是std::set将其内部结构基于其元素的值,如果更改这些值,内部结构将变为无效,并且会发生可怕的事情。

相关问题