迭代器声明:"不包含类型"

dtg*_*dtg 4 c++ queue iterator stl

我很难理解为什么我会收到此错误.我指的是Josuttis的STL书和其他资源,似乎我在下面声明我的迭代器的方式应该有效:

#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
Run Code Online (Sandbox Code Playgroud)

但是我声明我的const_iterator的行会生成以下编译器错误:

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
Run Code Online (Sandbox Code Playgroud)

在类中声明一个导致错误的迭代器有什么特别之处吗?

jua*_*nza 7

容器适配器std::queue没有可公开访问的迭代器.由于std::queue<int>隐藏在LRU实现中,您可以考虑使用std::deque<int>替代.std::dequestd::queue引擎盖下使用的默认容器,因此使用它不会导致性能损失.我认为只要不在LRU接口中公开非队列操作就可以安全使用它.