This method:
void LRU::displayQueue() const
{
for(iter = m_buffer.begin(); iter != m_buffer.end(); ++iter)
std::cout << (*iter) << " ";
std:: cout << std::endl;
}
results in the following error:
lru.cpp:58: error: passing 'const std::_Deque_iterator<int, const int&, const int*>' as 'this' argument of 'std::_Deque_iterator<int, const int&, const int*>& std::_Deque_iterator<int, const int&, const int*>::operator=(const std::_Deque_iterator<int, const int&, const int*>&)' discards qualifiers
m_buffer and iter are declared in my header file, where the buffer is declared as a deque of type int and iter is a constant iterator:
// ...
std::deque<int> m_buffer;
std::deque<int>::const_iterator iter;
// ...
Removing the const in the displayQueue method will eliminate the compiler error, but since this function shouldn’t modify any data in the deque, I want to make this explicit by keeping my code “const-correct”. Why would this result in an error, when my iterator is a const_iterator?
You’re modifying the object by setting the value of your const iterator member variable, thus invalidating the const restriction of your function. Use a local variable.
A
const_iteratoronly means the iterator cannot modify the sequence, but you’re not modifying the sequence, you modifying the member variable iterator itself.