![](/img/trans.png)
[英]Compiler can't “convert” between Vector_const_iterator and Vector_iterator, even though methods for both are available
[英]Getting an error about a _Vector_const_iterator not being convertible to a _Vector_iterator
我目前是C ++编程的新手,我正在尝试制作数独求解器。 但是,我在返回单元格的候选列表(单元格可能的值列表)的方法上遇到了麻烦。 候选列表是向量。 这是我目前尝试执行的操作,但是出现错误:
int Cell::getCandidateList(void) const
{
int i;
for (vector<int>::iterator j = m_candidateList.begin(); j < m_candidateList.end(); j++)
{
i = *j;
}
return i;
}
这是在头文件中声明的方式:
int getCandidateList(void) const; //create method to get candidate list
该错误似乎在m_candidateList.begin上,该错误说:
严重性代码说明项目文件行抑制状态错误(活动)不存在从“ std :: _ Vector_const_iterator >>”到“ std :: _ Vector_iterator >>”的合适的用户定义转换
好吧,首先,您不会从此函数返回向量,而只是重复地重新分配一个整数值... :-(
但是对于错误,您将遇到:您正试图将迭代器强制为非常量向量迭代器,通过该迭代器可以修改元素-这不是您想要的。 尝试:
for (vector<int>::const_iterator j = m_candidateList.begin(); j < m_candidateList.end(); j++)
要么:
for (auto j = m_candidateList.begin(); j < m_candidateList.end(); j++)
或更好,使用C ++ 11语法:
for (const auto& e : m_candidateList) { }
...在这种情况下,在每次迭代中, e
是对向量中连续整数的常量引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.