Iterator declaration: "does not contain type"

It’s hard for me to understand why I get this error. I mean both the Josuttis STL book and other resources, and it seems that the way I declared my iterator below should work:

#ifndef LRU_H #define LRU_H #include <queue> #include <iterator> class LRU { public: LRU(); // default constructor LRU(int); // constructor with argument ~LRU(); // destructor // Methods // void enqueue(int); // add datum to the queue void dequeue(); // remove datum from the queue void replace(); // replacement algorithm void displayQueue() const; // display contents of queue private: // Member Data // const int MAX_SIZE; int m_currentCount; std::queue<int> m_buffer; std::queue<int>::const_iterator iter; }; #endif 

But the line where I declare my const_iterator generates the following compiler error:

 In file included from main.cpp:10: lru.h:41: error: 'const_iterator' in class 'std::queue<int, std::deque<int, std::allocator<int> > >' does not name a type In file included from lru.cpp:10: lru.h:41: error: 'const_iterator' in class 'std::queue<int, std::deque<int, std::allocator<int> > >' does not name a type lru.cpp: In constructor 'LRU::LRU()': lru.cpp:17: error: class 'LRU' does not have any field named 'm_pos' lru.cpp: In constructor 'LRU::LRU(int)': lru.cpp:23: error: class 'LRU' does not have any field named 'm_pos' Compilation exited abnormally with code 1 at Thu Nov 15 10:47:31 

Is there anything special about the iterator declaration in the class that leads to the error?

+4
source share
1 answer

The container adapter std::queue does not have public iterators. Since std::queue<int> is hidden in the LRU implementation, you can use std::deque<int> instead. std::deque is the default container used by std::queue under the hood, so you can incur a performance penalty using it. I think it’s safe to use it if you do not make queueless operations public in your LRU interface.

+7
source

Source: https://habr.com/ru/post/1446309/


All Articles