akappa
akappa

Reputation: 10490

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?

Upvotes: 0

Views: 688

Answers (2)

user2100815
user2100815

Reputation:

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?

Upvotes: 5

iammilind
iammilind

Reputation: 70096

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;

Upvotes: 2

Related Questions