2014-11-05 188 views
1

我想知道是否有人可以幫我修復此錯誤。我仔細看了看,看不出有什麼可能出錯。編譯器被點到線類型'_wrap_iter <pointer>'的非常量左值引用無法綁定到無關類型的值

while (_hasNextAttribute(it1, it2, thisAttribute)) 
以下代碼

bool HtmlProcessor::_processTag(std::string::const_iterator it1, const std::string::const_iterator it2, node & nd) 
{ 
    /* 
     [it1, it2): iterators for the range of the string 
       nd: node in which classes and ids of the tage are stored 

     Returns true or false depending on whether a problem was encountered during the processing. 
    */ 


    std::string elementType(""); 
    while (_elementTypeChars.find(*it1) != std::string::npos && it1 != it2) elementType.push_back(*it1++); 
    if (elementType.empty()) return false; 
    nd.element_type = elementType; 


    std::vector<std::pair<std::string, std::string>> attributes; 
    const std::pair<std::string, std::string> thisAttribute; 
    while (_hasNextAttribute(it1, it2, thisAttribute)) 
     attributes.push_back(thisAttribute); 



    return true; 
} 

bool HtmlProcessor::_hasNextAttribute(std::string::iterator & it1, const std::string::iterator & it2, const std::pair<std::string, std::string> attrHolder) 
{ 

.... 

和跟它

非const左值參考輸入「_wrap_iter」不能結合至不相關的一個值類型'_wrap_iter'

+0

顯示'_hasNextAttribute'(及其引用的任何其他類,例如'_wrap_iter')的定義 – 2014-11-05 05:49:20

+1

雖然我猜測錯誤是它需要'thisAttribute'而不是'const' – 2014-11-05 05:49:50

回答

0

當我嘗試編譯你的代碼時,編譯器(VS 2013)抱怨const迭代器it1無法轉換爲std::string::iterator &。確切的錯誤消息:

1> CPP-Test.cpp的(36):錯誤C2664:「bool的_hasNextAttribute(STD :: _ St​​ring_iterator >> &,常量性病:: _ St​​ring_iterator >> &,常量性病::對)的std :: _ St​​ring_const_iterator >>」不能轉換參數1 '' 到 '的std :: _ St​​ring_iterator >> &'

基本上,你有兩個選擇:

  • 選項1:it1it2都是非const

    bool _processTag(std::string::iterator it1, const std::string::iterator it2, node & nd) 
    
  • 選項2:_hasNextAttribute()需要常量迭代器

    bool _hasNextAttribute(std::string::const_iterator & it1, const std::string::const_iterator & it2, const std::pair<std::string, std::string> attrHolder) 
    

一切編譯罰款對我來說,當我申請的其中一個選項(不能同時當然)。

可以選擇是否需要常量迭代器。 _hasNextAttribute()看起來像一個信息方法給我,即提供信息,不會改變任何東西。所以我猜const const iterator應該是OK的。

相關問題