Strange thing while using templates

Discussion in 'C++' started by drop669@gmail.com, Sep 8, 2007.

  1. Guest

    Hi.
    I have this piece of code:

    =====================================
    #include <map>

    template <class T> class dict
    {
    private:

    std::map<DWORD, T> m_d;

    [ ... ]

    void enum_keys_as_dwords (DWORD *out)
    {
    DWORD *o=out;

    for (std::map<DWORD, T>::iterator it=m_d.begin(); it!=m_d.end(); it+
    +)
    {
    *o=it->first;
    o++;
    };
    };
    };
    =====================================

    While compiling this in MinGW, it says:

    =====================================
    dict:91: error: expected `;' before "it"
    =====================================

    (line 91 is the line where "for (...)" is located).

    I'm really don't know what to do. The problem is probably in using
    type T. Where I mistaken?
     
    , Sep 8, 2007
    #1
    1. Advertising

  2. * :
    > Hi.
    > I have this piece of code:
    >
    > =====================================
    > #include <map>
    >
    > template <class T> class dict
    > {
    > private:
    >
    > std::map<DWORD, T> m_d;
    >
    > [ ... ]
    >
    > void enum_keys_as_dwords (DWORD *out)
    > {
    > DWORD *o=out;
    >
    > for (std::map<DWORD, T>::iterator it=m_d.begin(); it!=m_d.end(); it+
    > +)
    > {
    > *o=it->first;
    > o++;
    > };
    > };
    > };
    > =====================================
    >
    > While compiling this in MinGW, it says:
    >
    > =====================================
    > dict:91: error: expected `;' before "it"
    > =====================================
    >
    > (line 91 is the line where "for (...)" is located).
    >
    > I'm really don't know what to do. The problem is probably in using
    > type T. Where I mistaken?


    std::map<DWORD,T>::iterator could in principle be anything, depending on
    the type T. So you need to inform the compiler that it's a type. You
    do that by adding the word 'typename' in front.

    That said, it would be a good idea to use a std::vector instead of a raw
    array, and also to make that pure accessor a 'const' member function
    (probably 'DWORD' is a Windows API name, but in general, reserve all
    uppercase for macros).

    Then the function would look like

    void get_dwords( std::vector<DWORD>& result ) const
    {
    typedef typename std::map<DWORD, T>::const_iterator Iterator;
    std::vector<DWORD> words;
    for( Iterator it = m_d.begin(); it != m_d.end(); ++it )
    {
    words.push_back( it->first );
    }
    words.swap( result );
    }

    Note that this way is in general more exception safe as well, not just
    more safe against buffer overflows, null-pointer and other problems
    associated with raw arrays and pointers.

    You can also provide a convenience wrapper relying on Return Value
    Optimization (RVO), which most relevant compilers provide:

    std::vector<DWORD> dwords() const
    {
    std::vector<DWORD> result;
    get_dwords( result );
    return result;
    }

    The naming convention employed here is that a command-like function's
    name 'get_dwords' says what it does, not how (e.g. enumeration) it does
    it, and that a function-like function's name 'dwords' says what result
    it delivers. That makes the calling code easier to read and comprehend.

    Cheers, & hth.,

    - Alf
     
    Alf P. Steinbach, Sep 8, 2007
    #2
    1. Advertising

  3. Jerry Coffin Guest

    In article <>,
    says...
    > Hi.
    > I have this piece of code:


    [ code elided ]

    > =====================================
    >
    > While compiling this in MinGW, it says:
    >
    > =====================================
    > dict:91: error: expected `;' before "it"
    > =====================================
    >
    > (line 91 is the line where "for (...)" is located).


    I don't see anything wrong, at least in the code you posted. It's
    _possible_ there's a problem in the code you elided, and it's just not
    being diagnosed until you reach this point. Quite frankly, that looks
    pretty unlikely -- it takes a fairly strange error for the diagnostic to
    be delayed into the next function (though mis-matched braces can lead to
    it getting confused about where one function ends and the next starts).
    Otherwise, it looks to me like a compiler error.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Sep 8, 2007
    #3
  4. red floyd Guest

    Jerry Coffin wrote:
    > In article <>,
    > says...
    >> Hi.
    >> I have this piece of code:

    >
    > [ code elided ]
    >
    >> =====================================
    >>
    >> While compiling this in MinGW, it says:
    >>
    >> =====================================
    >> dict:91: error: expected `;' before "it"
    >> =====================================
    >>
    >> (line 91 is the line where "for (...)" is located).

    >
    > I don't see anything wrong, at least in the code you posted. It's
    > _possible_ there's a problem in the code you elided, and it's just not
    > being diagnosed until you reach this point. Quite frankly, that looks
    > pretty unlikely -- it takes a fairly strange error for the diagnostic to
    > be delayed into the next function (though mis-matched braces can lead to
    > it getting confused about where one function ends and the next starts).
    > Otherwise, it looks to me like a compiler error.
    >


    As Alf said, OP needs a "typename" ...

    for (typename std::map<...>::iterator it = ...)
     
    red floyd, Sep 8, 2007
    #4
  5. Jerry Coffin Guest

    In article <X7DEi.3755$>,
    says...

    [ ... ]

    > for (typename std::map<...>::iterator it = ...)


    Yup -- I still miss that all too often...

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Sep 9, 2007
    #5
  6. Barry Guest

    Alf P. Steinbach wrote:
    >
    > std::map<DWORD,T>::iterator could in principle be anything, depending on
    > the type T. So you need to inform the compiler that it's a type. You
    > do that by adding the word 'typename' in front.


    alf, did you really ever do that? :)
    for (typename std::map<key, value>::iterator it; ...)

    the typename issue is only with template parameter

    template <class Container>
    void f(Contanier const& c)
    {
    typename Container::iterator it;
    }




    --
    Thanks
    Barry
     
    Barry, Sep 9, 2007
    #6
  7. Barry Guest

    Barry wrote:
    > Alf P. Steinbach wrote:
    >>
    >> std::map<DWORD,T>::iterator could in principle be anything, depending
    >> on the type T. So you need to inform the compiler that it's a type.
    >> You do that by adding the word 'typename' in front.

    >
    > alf, did you really ever do that? :)
    > for (typename std::map<key, value>::iterator it; ...)
    >
    > the typename issue is only with template parameter
    >
    > template <class Container>
    > void f(Contanier const& c)
    > {
    > typename Container::iterator it;
    > }
    >


    Sorry, may bad, I did't see that in the OP
    there's a template parameter T
    ;-)

    --
    Thanks
    Barry
     
    Barry, Sep 9, 2007
    #7
    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. JKop
    Replies:
    3
    Views:
    492
  2. WangE
    Replies:
    0
    Views:
    312
    WangE
    Nov 17, 2006
  3. recover
    Replies:
    2
    Views:
    829
    recover
    Jul 25, 2006
  4. JohnQ
    Replies:
    104
    Views:
    1,675
    JohnQ
    Jun 16, 2007
  5. Zam
    Replies:
    1
    Views:
    257
    Mark Schupp
    Mar 14, 2005
Loading...

Share This Page