Connecting Tech Pros Worldwide Help | Site Map

A strange problem of template

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 18th, 2007, 03:15 PM
Zheng Da
Guest
 
Posts: n/a
Default 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


  #2  
Old July 18th, 2007, 03:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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


  #3  
Old July 18th, 2007, 08:15 PM
Zheng Da
Guest
 
Posts: n/a
Default 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

  #4  
Old July 18th, 2007, 08:25 PM
Victor Bazarov
Guest
 
Posts: n/a
Default 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


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

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.