简体   繁体   中英

C++: error while declaring a multimap iterator

I have a stupid problem due to my lack of knowledge about C++ templates.

I have a template class Token and a template class Task.

A Task contains some Token* inside a multimap; and I want to iterate over them.

So, in one of my function, I wrote:

template <typename C>
void Task<C>::f() {

    // some code...
    multimap<string, Token<C>* >::iterator it;

}

but I get this compilation error from g++:

src/structures.cpp:29: error: expected ‘;’ before ‘it’

if I put Token or something like that, it compiles.

Where's the error?

You want:

 typename  multimap<string, Token<C>* >::iterator it;

That error is so common that I think the compiler writers should make it:

error: expected ‘;’ before ‘it’ - did you forget a typename?

I follow a rule always,

"Whenever you are inside a template function (including its return type) and you are declaring some variable using scope resolution operator ("::"), then always put a typename."

Declare your variable as,

typename multimap<string, Token<C>* >::iterator it;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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