简体   繁体   English

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

[英]Errors when trying to print out vector of list of objects?

So I'm trying ti print out a vector of list of objects I have in a hash table with the following code but I keep getting these errors and I'm not sure why... 所以我试图用下面的代码用哈希表打印出我在哈希表中拥有的对象列表的向量,但是我不断收到这些错误,我不确定为什么...

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

Here is the snippet of my class featuring the print function: 这是我班的具有打印功能的代码片段:

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

};

And here's how I overload the ostream operator<< (in the Symbol Class): 这是我如何重载ostream运算符<<(在Symbol类中):

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

If you read the error carefully, it is saying that this line: 如果您仔细阅读该错误,则表示此行:

list<HashedObj>::iterator li;

Should read as: 应该读为:

typename list<HashedObj>::iterator li;

Basically, the you need to tell the compiler that you are dealing with a type. 基本上,您需要告诉编译器您正在处理类型。 See this question or this question for more details on what is going on. 有关正在发生的事情的更多详细信息,请参阅此问题此问题

You might have other errors, but you need to resolve these first. 您可能还有其他错误,但是需要首先解决。

SeparateChaining.h:165:13: error: need 'typename' before 'std::list<HashedObj>::iterator' because 'std::list<HashedObj>' is a dependent scope

The compiler cannot determine if list<HashedObj> is a static field or a type, so it assumes that list<HashedObj> is a field causing these syntax errors. 编译器无法确定list<HashedObj>是静态字段还是类型,因此假定list<HashedObj>是导致这些语法错误的字段。 You have to place a typename in front of the declaration to convince the compiler otherwise. 您必须在声明的前面放置一个typename ,以说服编译器。 It should look like: 它应该看起来像:

typename list<HashedObj>::iterator li;

Check out this similar post 看看这个类似的帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM