4

このエラーが発生する理由を理解するのに苦労しています。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

しかし、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

エラーが発生するクラスでイテレータを宣言することについて何か特別なことはありますか?

4

1 に答える 1

7

コンテナアダプタstd::queueには、公的にアクセス可能なイテレータはありません。std::queue<int>は実装に隠されているため、代わりにLRU使用することを検討できますstd::deque<int>std::dequeは内部で使用されるデフォルトのコンテナstd::queueであるため、これを使用してもパフォーマンスが低下することはありません。インターフェイスで非キュー操作を公開しない限り、これを使用しても安全だと思いLRUます。

于 2012-11-15T18:56:05.753 に答える