繁体   English   中英

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

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

我想知道是否有人可以帮助我解决此错误。 我已经查看了一下,看不到什么可能出了问题。 编译器指向该行

    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_iter'的值

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

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

基本上,您有两种选择:

  • 选项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迭代器应该可以。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM