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;
精彩评论