迭代器的声明:"does not contain a type"
Declaration of iterator: "does not contain a type"
我很难理解为什么会出现此错误。我指的是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
在导致错误的类中声明迭代器有什么特别之处吗?
容器
适配器std::queue
没有可公开访问的迭代器。由于std::queue<int>
隐藏在LRU
实现中,因此可以考虑改用std::deque<int>
。 std::deque
是std::queue
使用的默认容器,因此使用它不会对性能造成任何影响。我认为只要您不在您的LRU
界面中公开非队列操作,使用它就是安全的。
相关文章:
- 使用 [] 运算符时"binding reference of type discards qualifiers"
- 在 QVector<std::unique_ptr 上使用 std::find<Type>>
- 在类型和包装器之间reinterpret_cast是否安全<Type>?
- 使用 cmake 的 Linux 终端上的"Conversion to non-scalar type is requested"错误
- 控制到达非空函数clang(-Wreturn-type)的末尾
- std::unordered_map 类型对象声明期间出现"field has incomplete type"错误
- 将系数存储在头文件的数组中("does not name a type"错误)
- 尝试打开 ifstream 时出现"Incomplete type"错误
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- 在"结构提升::enable_if<提升::is_pod<T>,无效>"中没有名为"type"的类型
- "Missing type specifier - int assumed"无法通过向主函数添加"return 0"来解决
- OpenCV CV_16F type
- C++ "错误:在'类 std::result_of< ... >"中没有名为'type'的类型"
- 将内存分配返回值强制转换为 TYPE 数组
- C++ 一个lambda浅拷贝const Type&如果它被赋予一个命名捕获,如[copy=val](){}?
- reference_wrapper导致"incomplete type is not allowed"
- 继承类时"invalid use of incomplete type ‘class tree_node_t’"
- 由于"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int",我现在无法编
- 如何解决"'mutex' in namespace 'std' does not name a type"?
- 迭代器的声明:"does not contain a type"