非常量左值引用类型&#39; _wrap_iter <pointer>&#39;无法绑定到不相关类型的值</pointer>

时间:2014-11-05 05:45:47

标签: c++

我想知道是否有人可以帮我修复此错误。我已经查看过,无法查看可能出现的问题。编译器指向

    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)
{

....

并且正在说

非常量左值引用类型&#39; _wrap_iter&#39;无法绑定到不相关类型的值&#39; _wrap_iter&#39;

1 个答案:

答案 0 :(得分:0)

当我尝试编译代码时,编译器(VS 2013)抱怨const迭代器it1无法转换为std::string::iterator &。确切的错误消息:

  

1&gt; Cpp-Test.cpp(36):错误C2664:'bool _hasNextAttribute(std :: _ St​​ring_iterator&gt;&gt;&amp;,const std :: _ St​​ring_iterator&gt;&gt;&amp;,const std :: pair)':无法从'std :: _ St​​ring_const_iterator&gt;&gt;'转换参数1到'std :: _ St​​ring_iterator&gt;&gt; &安培;'

基本上你有两个选择:

  • 选项1:it1it2是非常量

    bool _processTag(std::string::iterator it1, const std::string::iterator it2, node & nd)
    
  • 选项2:_hasNextAttribute()采用const迭代器

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

当我应用其中一个选项时(当然不是两个选项),所有内容都适合我。

选择是否需要const迭代器。 _hasNextAttribute()看起来像是一种信息方法,即提供信息,而不是改变任何东西。所以我猜const迭代器应该没问题。

相关问题