2012-03-14 13 views
2

考慮下面的代碼沒有法律轉換:錯誤C2663: '的std :: _哈希<_Traits> ::插入':3個重載有this指針

#include <unordered_map> 

struct A {}; 

struct T 
{ 
    std::unordered_map<std::string, A> _map; 
}; 

struct L 
{ 
    std::shared_ptr<const T> _c; 
}; 


class f { 
    void oid (std::shared_ptr<L> l, std::string st, A a) { 
     l->_c->_map.insert(std::make_pair(st,a)); 
    } 
}; 

在編譯過程中,它會引發以下錯誤:

error C2663: 'std::_Hash<_Traits>::insert' : 3 overloads have no legal conversion for 'this' pointer with [ _Traits=std::tr1::_Umap_traits,std::equal_to>,std::allocator>,false> ]

我tryed以去除std::shared_ptr<const T> _c;(不,我認爲它很重要),但它構建了一些其他錯誤const ...

謝謝你的他LP!

+0

如果您嘗試刪除*實際上很重要的'const',那麼錯誤是什麼? – 2012-03-14 16:22:31

回答

4

您正試圖插入您聲明爲constunordered_map,這是不允許的。爲什麼_c裏面的L a shared_ptr<const T> ???這使得由共享指針管理的對象上的_map有效地成爲常量對象,您將無法修改它。

+0

哎呀!由於複製和粘貼,我犯了同樣的錯誤,但我忽略了const! – BuvinJ 2015-04-15 16:31:39

相關問題