开发者

C++: error while declaring a multimap iterator

开发者 https://www.devze.com 2023-03-02 02:53 出处:网络
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.

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(开发者_如何学Python) {

    // 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;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号