所以我想TI打印出對象列表的向量我在用下面的代碼的哈希表,但我不斷收到這些錯誤,我不知道爲什麼......嘗試打印出對象列表的向量時出錯?
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的操作< <(在符號類):
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;
}
您能不能告訴全班? 或者你可以首先在列表 :: iterator之前添加typename作爲編譯器說的嗎? –