A strange problem of template

Discussion in 'C++' started by Victor Bazarov, Jul 18, 2007.

  1. Zheng Da wrote:
    > 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 , T> table;
    > public:
    > void find(const key &k){
    > map<key,T>::iterator it=table.find(k); <=the error
    > }
    > };
    >
    > int main()
    > {
    > hashmap<int,int> hm;
    > }
    >
    > 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
     
    Victor Bazarov, Jul 18, 2007
    #1
    1. Advertising

  2. Zheng Da wrote:
    > Victor Bazarov wrote:
    >> Zheng Da wrote:
    >>> 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 , T> table;
    >>> public:
    >>> void find(const key &k){
    >>> map<key,T>::iterator it=table.find(k); <=the error
    >>> }
    >>> };
    >>>
    >>> int main()
    >>> {
    >>> hashmap<int,int> hm;
    >>> }
    >>>
    >>> 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
     
    Victor Bazarov, Jul 18, 2007
    #2
    1. Advertising

  3. Victor Bazarov

    Zheng Da Guest

    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 , T> table;
    public:
    void find(const key &k){
    map<key,T>::iterator it=table.find(k); <=the error
    }
    };

    int main()
    {
    hashmap<int,int> hm;
    }

    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
     
    Zheng Da, Jul 19, 2007
    #3
  4. Victor Bazarov

    Zheng Da Guest

    Victor Bazarov wrote:
    > Zheng Da wrote:
    >> 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 , T> table;
    >> public:
    >> void find(const key &k){
    >> map<key,T>::iterator it=table.find(k); <=the error
    >> }
    >> };
    >>
    >> int main()
    >> {
    >> hashmap<int,int> hm;
    >> }
    >>
    >> 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
     
    Zheng Da, Jul 19, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Theis
    Replies:
    2
    Views:
    503
    Chris Theis
    Jul 24, 2003
  2. tom_usenet
    Replies:
    0
    Views:
    568
    tom_usenet
    Jul 24, 2003
  3. Replies:
    1
    Views:
    2,150
    Gianni Mariani
    Jun 8, 2007
  4. Peng Yu
    Replies:
    3
    Views:
    809
    Thomas J. Gritzan
    Oct 26, 2008
  5. nguillot
    Replies:
    5
    Views:
    563
Loading...

Share This Page