Connecting Tech Pros Worldwide Forums | Help | Site Map

A strange problem of template

Zheng Da
Guest
 
Posts: n/a
#1: Jul 18 '07
Hello,

I want a use the class of map in a class with template,
and the type of elements in map is specified by the parameter of the
class's template.
For example,
template <typename key, typename T>
class hashmap
{
map<key , Ttable;
public:
void find(const key &k){
map<key,T>::iterator it=table.find(k); <=the error
}
};

int main()
{
hashmap<int,inthm;
}

When I compile it, I get the error
test.cc:33: error: expected `;' before 'it'
I don't get it. Why I can use map in this class, but I can't define its
iterator?

Zheng Da

--
Posted via a free Usenet account from http://www.teranews.com


Victor Bazarov
Guest
 
Posts: n/a
#2: Jul 18 '07

re: A strange problem of template


Zheng Da wrote:
Quote:
I want a use the class of map in a class with template,
and the type of elements in map is specified by the parameter of the
class's template.
For example,
template <typename key, typename T>
class hashmap
{
map<key , Ttable;
public:
void find(const key &k){
map<key,T>::iterator it=table.find(k); <=the error
}
};
>
int main()
{
hashmap<int,inthm;
}
>
When I compile it, I get the error
test.cc:33: error: expected `;' before 'it'
I don't get it. Why I can use map in this class, but I can't define
its iterator?
Read up on 'typename' keyword and where it's supposed to be used.
(Hint: there are FAQ entires that have it and there is more than one
use of that keyword)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Zheng Da
Guest
 
Posts: n/a
#3: Jul 18 '07

re: A strange problem of template


Victor Bazarov wrote:
Quote:
Zheng Da wrote:
Quote:
>I want a use the class of map in a class with template,
>and the type of elements in map is specified by the parameter of the
>class's template.
>For example,
>template <typename key, typename T>
>class hashmap
>{
>map<key , Ttable;
>public:
>void find(const key &k){
>map<key,T>::iterator it=table.find(k); <=the error
>}
>};
>>
>int main()
>{
>hashmap<int,inthm;
>}
>>
>When I compile it, I get the error
>test.cc:33: error: expected `;' before 'it'
>I don't get it. Why I can use map in this class, but I can't define
>its iterator?
>
Read up on 'typename' keyword and where it's supposed to be used.
(Hint: there are FAQ entires that have it and there is more than one
use of that keyword)
>
V
I'm sorry, I really didn't find the solution.
You mean http://www.parashift.com/c++-faq-lite/templates.html?
I didn't find the similar question there.
I tried to use 'typename' as keyword to search c++ faq, and still didn't
find the similar questions.

Zheng Da

--
Posted via a free Usenet account from http://www.teranews.com

Victor Bazarov
Guest
 
Posts: n/a
#4: Jul 18 '07

re: A strange problem of template


Zheng Da wrote:
Quote:
Victor Bazarov wrote:
Quote:
>Zheng Da wrote:
Quote:
>>I want a use the class of map in a class with template,
>>and the type of elements in map is specified by the parameter of the
>>class's template.
>>For example,
>>template <typename key, typename T>
>>class hashmap
>>{
>>map<key , Ttable;
>>public:
>>void find(const key &k){
>>map<key,T>::iterator it=table.find(k); <=the error
>>}
>>};
>>>
>>int main()
>>{
>>hashmap<int,inthm;
>>}
>>>
>>When I compile it, I get the error
>>test.cc:33: error: expected `;' before 'it'
>>I don't get it. Why I can use map in this class, but I can't define
>>its iterator?
>>
>Read up on 'typename' keyword and where it's supposed to be used.
>(Hint: there are FAQ entires that have it and there is more than one
> use of that keyword)
>>
>V
>
I'm sorry, I really didn't find the solution.
You mean http://www.parashift.com/c++-faq-lite/templates.html?
I didn't find the similar question there.
I tried to use 'typename' as keyword to search c++ faq, and still
didn't find the similar questions.
You know what, I don't want to prove you wrong or prove myself wrong.
Just do

typename map<key,T>::iterator it = ...

And get a decent book on C++ templates that explains the need for the
'typename' keyword, OK?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Closed Thread