0

我目前是 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>>”的合适的用户定义转换

4

1 回答 1

1

好吧,首先,你没有从这个函数返回一个向量,你只是重复地重新分配一个整数值...... :-(

但至于你得到的错误:你试图强制你的迭代器是一个非常量向量迭代器,通过它可以修改元素——这不是你想要的。尝试:

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都是对向量中连续整数的常量引用。

于 2017-03-26T17:07:33.263 回答