简体   繁体   中英

Getting an error about a _Vector_const_iterator not being convertible to a _Vector_iterator

I am currently new to C++ programming and I am trying to make a sudoku solver. However, I am having trouble with the method which returns the candidate list for a cell (the list of possible values the cells could be). The candidate list is a vector. This is how I have currently tried to do it, however it is coming up with an error:

 int Cell::getCandidateList(void) const
{
int i;
for (vector<int>::iterator j = m_candidateList.begin(); j <       m_candidateList.end(); j++)
{
    i = *j;
}
  return i;
 }

And this is how it is declared in the header file:

 int getCandidateList(void) const; //create method to get candidate list

The error seems to be on the m_candidateList.begin and the error says:

Severity Code Description Project File Line Suppression State Error (active) no suitable user-defined conversion from "std::_Vector_const_iterator>>" to "std::_Vector_iterator>>" exists

Well, first of all, you're not returning a vector from this function, you're just reassigning an integer value repeatedly... :-(

But as for the error you're getting: You're trying to force your iterator to be a non-const vector iterator, through which elements can be modified - which is not what you want. Try:

for (vector<int>::const_iterator j = m_candidateList.begin(); j < m_candidateList.end(); j++)

or:

for (auto j = m_candidateList.begin(); j < m_candidateList.end(); j++)

or better yet, using C++11 syntax:

for (const auto& e : m_candidateList) { }

... in which case, in each iteration e is a constant reference to a consecutive integer in the vector.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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