Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 迭代器声明:“引用;不包含类型“;_C++_Stl_Iterator_Queue - Fatal编程技术网

C++ 迭代器声明:“引用;不包含类型“;

C++ 迭代器声明:“引用;不包含类型“;,c++,stl,iterator,queue,C++,Stl,Iterator,Queue,我很难理解为什么会出现这个错误。我指的是Josuttis的STL书和其他参考资料,我在下面声明迭代器的方式似乎应该是可行的: #ifndef LRU_H #define LRU_H #include <queue> #include <iterator> class LRU { public: LRU(); // default constructor LRU(int);

我很难理解为什么会出现这个错误。我指的是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
\ifndef LRU\H
#定义LRU_H
#包括
#包括
类LRU
{
公众:
LRU();//默认构造函数
LRU(int);//带参数的构造函数
~LRU();//析构函数
//方法
//
void enqueue(int);//向队列添加数据
void dequeue();//从队列中删除数据
void replace();//替换算法
void displayQueue()const;//显示队列的内容
私人:
//成员数据
//
const int MAX_SIZE;
int m_currentCount;
std::队列m_缓冲区;
std::queue::const_迭代器iter;
};
#恩迪夫
但是我声明const_迭代器的那一行生成了以下编译器错误:

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
包含在main.cpp:10中的文件中:
lru.h:41:错误:“std::queue”类中的“const_iterator”未命名类型
在lru.cpp中包含的文件中:10:
lru.h:41:错误:“std::queue”类中的“const_iterator”未命名类型
lru.cpp:在构造函数“lru::lru()”中:
lru.cpp:17:错误:类“lru”没有任何名为“m_pos”的字段
lru.cpp:在构造函数“lru::lru(int)”中:
lru.cpp:23:错误:类“lru”没有任何名为“m_pos”的字段
编译在11月15日星期四10:47:31异常退出,代码为1

在导致错误的类中声明迭代器有什么特别的地方吗

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

我明白了。我可能早就知道了。考虑到队列数据结构的性质,我想这是有意义的。是否有一个只允许读取访问的队列内部迭代器?我只需要一个方法来显示队列的内容。不,您无权访问内部。但是您可以使用
std::deque
,并确保不公开任何非队列访问。