对类型 '_wrap_iter<pointer>' 的非常量左值引用无法绑定到不相关类型的值
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_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:
it1
和it2
是非常量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迭代器应该没问题。
相关文章:
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- 将成员函数的返回类型引用到C++中的自定义类
- 非类型引用参数可以在运行时修改,这是否意味着模板可以在运行时实例化?
- C++:类类型引用运算符=用法
- 将 const 类型引用对象注册为类成员对象C++
- 错误:绑定到类型引用会丢弃限定符
- 如果作为基类型引用传递,派生类型会解构吗
- 未分配返回未定义对象类型引用的 C++ 函数的返回值时会发生什么情况
- 类型引用的初始化无效
- C++ 从函数返回多个类型引用并将它们强制转换为我们需要的类型
- 函数常量返回类型:类型引用的初始化无效
- 将类型的嵌套类型引用为类模板的参数的问题
- 与返回类型(引用、常量引用)混淆
- 调用具有const键类型引用的std::指针集的count方法
- 返回运算符类型=-引用或值
- 非类型引用形参/实参
- c++中定义不同类型引用的区别
- 为什么返回类型引用输出流
- 非类型(引用)模板参数和链接
- 类型引用的初始化无效