這段代碼在2015年VS失敗(我沒有任何其他的編譯器測試它):爲什麼我不能在我的地圖中插入/插入?
class loader
{
bool parseMeta() const
{
for (auto it = meta_.begin(); it != meta_.end(); ++it)
{
std::string line(*it);
size_t n = line.find_first_of("=", 2);
if (n == std::string::npos) { return false; }
std::string key = line.substr(2, n);
std::string value = line.substr(n + 1);
kvp_.emplace(std::make_pair(key, value)); // <-- HERE
}
return true;
}
private:
std::vector<std::string> meta_;
std::map<std::string, std::string> kvp_;
};
給這個可愛的錯誤: error C2662: 'std::pair<std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const _Kty,_Ty>>>>,bool> std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>>::emplace<std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char>>,std::basic_string<char,std::char_traits<char>,std::allocator<char>>>>(std::pair<std::basic_string<char,std::char_traits<char>,std::allocator<char>>,std::basic_string<char,std::char_traits<char>,std::allocator<char>>> &&)': cannot convert 'this' pointer from 'const std::map<std::string,std::string,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>' to 'std::_Tree<std::_Tmap_traits<_Kty,_Ty,_Pr,_Alloc,false>> &
但是如果我移動kvp_
到線在標記的故障線之上(由<-- HERE
表示),則錯誤消失。
與insert
相同。我試過製作string
s const
但這沒有效果。爲什麼會發生?我的代碼有什麼問題?
這是正確的。我覺得愚蠢的是沒有意識到:-( –
理論上,錯誤信息中的'const'應該已經清楚了,現在如果我們只能弄清楚那些「const」中哪一個是明確的那個);) –
我過分關注'const map'到'_Tree'的轉換。 –