Pergunta

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):

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;
}
Foi útil?

Solução

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.

Outras dicas

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. You have to place a typename in front of the declaration to convince the compiler otherwise. It should look like:

typename list<HashedObj>::iterator li;

Check out this similar post

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top