2014-03-19 77 views
0

所以我想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; 
} 
+0

您能不能告訴全班? 或者你可以首先在列表 :: iterator之前添加typename作爲編譯器說的嗎? –

回答

0

如果你仔細閱讀錯誤,它說這行:

list<HashedObj>::iterator li; 

應閱讀:

typename list<HashedObj>::iterator li; 

基本上,你需要告訴你正在處理一個類型的編譯器。有關正在發生的更多詳情,請參見this questionthis question

您可能有其他錯誤,但您需要先解決這些錯誤。

+0

這工作!非常感謝 –

0
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; 

退房this similar post