对类型 '_wrap_iter<pointer>' 的非常量左值引用无法绑定到不相关类型的值

Non-const lvalue reference to type '_wrap_iter<pointer>' cannot bind to a value of unrelated type

本文关键字:类型 引用 不相关 绑定 gt iter wrap lt pointer 非常 常量      更新时间:2023-10-16

我想知道是否有人能帮我修复这个错误。我看了一遍,看不出可能出了什么问题。编译器指向行

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

并且说

对类型"_wrap_iter"的非常量左值引用无法绑定到不相关类型"_wrap_ter"的值

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

1> Cpp Test.Cpp(36):错误C2664:"bool _hasNextAttribute(std::_String_iterator>>&,const std::_String_iterator>>&aamp;,conststd::对)":无法将参数1从"std::_String_Test_iterator>>"转换为"std:

基本上你有两个选择:

  • 选项1:it1it2是非常量

    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)
    

当我应用其中一个选项(当然不是两个都应用)时,一切都很好。

可以选择是否在此处使用const迭代器。_hasNextAttribute()在我看来像是一种信息方法,即提供信息,而不是更改任何内容。所以我想const迭代器应该没问题。