【问题标题】:Errors when trying to print out vector of list of objects?尝试打印对象列表向量时出错?
【发布时间】:2014-04-25 03:26:30
【问题描述】:

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

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

这是我班的具有打印功能的 sn-p:

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

【问题讨论】:

  • 你能展示全班吗?或者你可以先在 list::iterator 之前添加 typename ,就像编译器说的那样?

标签: c++ list vector iterator hashtable


【解决方案1】:
SeparateChaining.h:165:13: error: need 'typename' before 'std::list<HashedObj>::iterator' because 'std::list<HashedObj>' is a dependent scope

编译器无法确定list&lt;HashedObj&gt; 是静态字段还是类型,因此它假定list&lt;HashedObj&gt; 是导致这些语法错误的字段。您必须在声明前放置 typename 以说服编译器否则。它应该看起来像:

typename list<HashedObj>::iterator li;

查看this similar post

【讨论】:

    【解决方案2】:

    如果你仔细阅读错误,就是说这一行:

    list<HashedObj>::iterator li;
    

    应该读作:

    typename list<HashedObj>::iterator li;
    

    基本上,您需要告诉编译器您正在处理一种类型。请参阅 this questionthis question 了解更多详情。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      • 1970-01-01
      • 2020-05-09
      • 2014-09-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多