尝试打印对象列表的矢量时出错
Errors when trying to print out vector of list of objects?
所以我试图用下面的代码打印出哈希表中对象列表的向量,但我一直收到这些错误,我不知道为什么。。。
SeparateChaining.h: In member function 'void HashTable<HashedObj>::print()':
SeparateChaining.h:165:13: error: need 'typename' before 'std::list<HashedObj>::iterator' because 'std::list<HashedObj>' is a dependent scope
SeparateChaining.h:165:39: error: expected ';' before 'li'
SeparateChaining.h:166:17: error: 'li' was not declared in this scope
SeparateChaining.h: In instantiation of 'void HashTable<HashedObj>::print() [with HashedObj = Symbol]':
Driver.cpp:72:21: required from here
SeparateChaining.h:165:13: error: dependent-name 'std::list<HashedObj>::iterator' is parsed as a non-type, but instantiation yields a type
SeparateChaining.h:165:13: note: say 'typename std::list<HashedObj>::iterator' if a type is meant
以下是我的类中具有打印功能的片段:
class HashTable
{
/// ....
void print()
{
for(int i = 0; i < theLists.size(); ++i)
{
list<HashedObj>::iterator li;
for(li = theLists[i].begin(); li != theLists[i].end(); ++li)
cout << "PLEASE WORK" << endl;
}
/*
for(auto iterator = oldLists.begin(); iterator!=oldLists.end(); ++iterator)
for(auto itr = theLists.begin(); itr!=theLists.end(); ++itr)
cout << *itr << endl;
*/
}
private:
vector<list<HashedObj>> theLists; // The array of Lists
};
下面是我如何重载ostream运算符<lt;(在符号类中(:
friend ostream & operator <<(ostream & outstream, Symbol & symbol) //overloaded to print out the the HashTable
{
int num = symbol.type;
string name = symbol.data;
outstream << name << " : " << num << "n";
return outstream;
}
如果你仔细阅读错误,它会说这一行:
list<HashedObj>::iterator li;
应读作:
typename list<HashedObj>::iterator li;
基本上,您需要告诉编译器您正在处理一个类型。有关正在发生的事情的更多详细信息,请参阅此问题或此问题。
您可能还有其他错误,但需要先解决这些错误。
SeparateChaining.h:165:13: error: need 'typename' before 'std::list<HashedObj>::iterator' because 'std::list<HashedObj>' is a dependent scope
编译器无法确定list<HashedObj>
是静态字段还是类型,因此它假定list<HashedObj>
是导致这些语法错误的字段。您必须在声明前面放置一个typename
来说服编译器。它应该看起来像:
typename list<HashedObj>::iterator li;
看看这个类似的帖子
相关文章:
- 读取文件的最后一行并输入到链接列表时出错
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 基于数组的列表 - 动态数组创建时出错
- 编译包含指向模板函数的指针的初始值设定项列表时,gcc 出错,但 clang 不出错
- 方法的参数列表出错
- 使用 C++ 的迭代目录列表出错
- 尝试访问列表时出错
- 使用列表 STL 时出错
- C2057构造函数初始化列表中的常量值定义出错
- 使用变量参数列表为传递的引用赋值(VS2010中出错)
- 创建模板树节点列表时出错
- 通过函数后从列表中删除项目时出错
- 链接列表的Push_back函数出错
- 泛型列表节点类中出错
- C++实现邻接列表时出错
- 运行列表的实现时出错
- 使用链接列表"Stop working"的 c++ 代码中出错
- g++4.8.2上列表方法参数默认初始化时出错
- 将大括号括起来的初始值设定项列表转换为类型时出错的含义
- 如果类构造函数的大括号闭合列表的大小错误,则编译时出错