尝试打印对象列表的向量时出错?

时间:2014-03-19 04:48:56

标签: c++ list vector iterator hashtable

所以我尝试使用以下代码打印出哈希表中的对象列表向量,但我不断收到这些错误,我不知道为什么......

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;&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;
}

2 个答案:

答案 0 :(得分:0)

如果您仔细阅读错误,则说明这一行:

list<HashedObj>::iterator li;

读作:

typename list<HashedObj>::iterator li;

基本上,您需要告诉编译器您正在处理类型。有关正在进行的操作的详细信息,请参阅this questionthis question

您可能有其他错误,但您需要先解决这些错误。

答案 1 :(得分: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