A strange problem of template 
July 18th, 2007, 03:15 PM
| | | A strange problem of template
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 | 
July 18th, 2007, 03:25 PM
| | | 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 | 
July 18th, 2007, 08:15 PM
| | | 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 | 
July 18th, 2007, 08:25 PM
| | | 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 | | Thread Tools | Search this Thread | | | |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 220,989 network members.
|